Rails 3 - find condition on a many to many through association
-
21-04-2021 - |
Frage
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.
Lösung
@tasks = User.find(params[:user_id]).tasks
should work fine.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow