activerecord - In rails 4 what is the correct way of using .where with associations? -


i in rails 4 , trying perform:

user.properties.where(:property_id => 2).first 

this produces sql error understand , know why cannot it. want know correct way find foreign key :has_many associations in rails 4.

similar (which works):

property.where(:id => 1).first  

try this:

user.joins(:properties).where('properties.id' => 2).first 

if want eager load properties use includes instead of joins.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -