开发者

Finding records when using has_many through associations

开发者 https://www.devze.com 2023-01-01 07:52 出处:网络
I have two models, Worker and Project, and they are connected with has_many through association. I manage to find all the projects which are related to a specific worker by writing the following code

I have two models, Worker and Project, and they are connected with has_many through association.

I manage to find all the projects which are related to a specific worker by writing the following code:

worker=Worker.find_by_id("some_id") 
worker.projects 

but I want the projects that I get to be only active projects (in the project model I have a status field)

I tried to do something like

worker.projects(:status_id=>'active')

but it didn’t work for me.

Can somebody tell me how I c开发者_如何转开发an do this?


Try:

worker.projects.all(:conditions => {:status_id => 'active'})


worker.projects.all(:conditions => {:status_id => 'active'})

will work. (answer edited after the comment)

0

精彩评论

暂无评论...
验证码 换一张
取 消