dormitkon
dormitkon

Reputation: 2526

ActiveRecord find with include

Let's say that Person has_many Addresses, and with this query I can go through the loop and use addresses for that peoples.

@people = Person.find(:all, :include => :addresses)

But, I want to include ONLY addresses where the user_id = 1 for example.

How to build query to do that?

P.S. New to RoR

Upvotes: 1

Views: 2985

Answers (1)

Sam 山
Sam 山

Reputation: 42865

@people = Person.find(:all, :include => :addresses, :conditions => ['user_id=?',params[:user_id]])

Make note that when you include you can only have one column of each or rails will give you an error about ambiguous find conditions.

Upvotes: 4

Related Questions