开发者

Referencing a belonged_to items owner in Rails / ActiveRecord query

开发者 https://www.devze.com 2023-01-20 17:50 出处:网络
Lets sa开发者_Python百科y I have a Person model and a Opinion model.Opinions belong to Person, a Person has many Opinions.Some people are shy.

Lets sa开发者_Python百科y I have a Person model and a Opinion model. Opinions belong to Person, a Person has many Opinions. Some people are shy.

I am trying to find a set of opinions where the person is 'not shy' or something along those lines. Is there a way to do this that does not involve finding all people who are not shy and then finding those people's opinions? I would like to do something like the pseudo-code below:

@opinions = Opinion.all.where_owner_person('shy = false')

this might be something obvious I have missed, but I can't seem to come up with the right phrasing in my searches to catch the answer.

thanks in advance.


This should work:

@opinions = Opinion.joins(:person).where(:people => { :shy => false })

ActiveRecord documentation has some more examples.

0

精彩评论

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