activerecord find through association

家住魔仙堡 提交于 2019-11-30 21:12:17

If you're using Rails 3.x, the following code would get the query result:

User.where(:username => "Paul").includes(:domains).where("domains.name" => "paul-domain").limit(1)

To inspect what happen, you can append .to_sql to above code.

If you're using Rails 2.x, you'd better write the raw sql query.

Dimitris

The following piece of code did the trick:

User.joins(:account).joins('INNER JOIN "domains" ON "accounts"."id" = \
"domains"."account_id"').where(:users => {"username" => "Paul"}).
where(:domains => {"name" => "paul-domain"})

Sorry about the formatting of this long line of code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!