Question

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.

Was it helpful?

Solution

@tasks = User.find(params[:user_id]).tasks

should work fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top