# Consultas avanzadas de ActiveRecord (Ruby on Rails) > Query interface, where, joins, includes, eager loading, problema N+1, transactions, locking (optimistic/pessimistic) - 28 preguntas de entrevista - Mid-Level - [Preguntas de entrevista: Ruby on Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista.md) ## 1. ¿Qué método de ActiveRecord recupera un registro por su clave primaria y lanza una excepción si no se encuentra? **Respuesta** El método find recupera un registro por su clave primaria y lanza una excepción ActiveRecord::RecordNotFound si el registro no existe. Esto garantiza que el código falle de forma explícita en lugar de continuar con un valor nil. Para evitar la excepción y devolver nil en su lugar, usa find_by(id: value). ## 2. ¿Cuál es la diferencia principal entre find_by y where.first en ActiveRecord? **Respuesta** find_by devuelve directamente un único registro o nil, mientras que where.first devuelve una relación ActiveRecord y luego llama a first para obtener el primer resultado. En la práctica, find_by genera una consulta optimizada con LIMIT 1, mientras que where podría cargar más datos antes de seleccionar el primer elemento. ## 3. ¿Cómo pasar múltiples condiciones al método where en ActiveRecord? **Respuesta** El método where acepta un hash con múltiples claves para especificar varias condiciones que se combinarán con AND. Por ejemplo, User.where(status: 'active', role: 'admin') genera WHERE status = 'active' AND role = 'admin'. Para condiciones OR, usa or o consultas SQL en bruto. ## 25 preguntas más disponibles - ¿Qué es el problema N+1 en ActiveRecord? - ¿Qué método usar para resolver el problema N+1 precargando las asociaciones? Regístrate gratis: https://sharpskill.dev/es/login ## Otros temas de entrevista Ruby on Rails - [Fundamentos de Ruby](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/ruby-basics.md): 25 preguntas, Junior - [Programación orientada a objetos en Ruby](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/ruby-oop.md): 20 preguntas, Junior - [Fundamentos de Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-fundamentals.md): 18 preguntas, Junior - [Routing & Controllers](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/routing-controllers.md): 22 preguntas, Junior - [Fundamentos de ActiveRecord](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/active-record-basics.md): 25 preguntas, Junior - [Views y plantillas ERB](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/views-erb-templates.md): 20 preguntas, Junior - [Asociaciones de ActiveRecord](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/active-record-associations.md): 24 preguntas, Mid-Level - [Formularios Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-forms.md): 20 preguntas, Mid-Level - [Autenticación y Autorización](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/authentication-authorization.md): 22 preguntas, Mid-Level - [Asset Pipeline moderno y frontend](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-asset-pipeline.md): 18 preguntas, Mid-Level - [Modo API de Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-api-mode.md): 20 preguntas, Mid-Level - [Pruebas con RSpec](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-testing-rspec.md): 24 preguntas, Mid-Level - [ActiveJob & Background Jobs](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/active-job-background-jobs.md): 20 preguntas, Mid-Level - [ActionCable & WebSockets](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/action-cable-websockets.md): 18 preguntas, Mid-Level - [ActionMailer](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/action-mailer.md): 18 preguntas, Mid-Level - [ActiveStorage](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/active-storage.md): 20 preguntas, Mid-Level - [Estrategias de caching](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/caching-strategies.md): 20 preguntas, Mid-Level - [Migraciones avanzadas](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/database-migrations-advanced.md): 20 preguntas, Mid-Level - [Rails Engines y apps modulares](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-engines-modular-apps.md): 18 preguntas, Senior - [Optimización del rendimiento](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/performance-optimization.md): 26 preguntas, Senior - [Patrones de diseño en Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-design-patterns.md): 22 preguntas, Senior - [Metaprogramación en Ruby](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/ruby-metaprogramming.md): 20 preguntas, Senior - [Seguridad en Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-security.md): 22 preguntas, Senior - [GraphQL con Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/graphql-rails.md): 20 preguntas, Senior - [Despliegue y Producción](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-deployment-production.md): 20 preguntas, Senior - [Monitoring & Logging](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/monitoring-logging.md): 20 preguntas, Senior - [Estrategias de actualización de Rails](https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/rails-upgrade-strategies.md): 18 preguntas, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/es/technologies/ruby-on-rails/preguntas-entrevista/active-record-queries