Rails 3 - find condition on a many to many through association

1.1k views Asked by At

I have three models: User, Task and Assignation. User has many tasks through assignation. Tasks has many users through assignation.

class User < ActiveRecord::Base
  has_many :assignations
  has_many :tasks, :through => :assignations 
  ...

class Task < ActiveRecord::Base
  has_many :assignations
  has_many :users, :through => :assignations

I have a partial which shows all the tasks of the selected user. How can I make the condition efficiently so I can get the collection of tasks?

i.e.

user_id = params[:user_id]
@tasks = Task.find(:all, :conditions=> .....)

Regards.

1

There are 1 answers

1
lucapette On BEST ANSWER
@tasks = User.find(params[:user_id]).tasks

should work fine.