2016-06-08 255 views

回答

1

Rails的做法是不干净的,但它可以让你在你的查询中明确。 在情况如上所述,你会做

Entry.joins(:blog).where(blogs: {name: "Beatles Blog"}) 

这是假设你的博客has_many :entries和你进入belongs_to :blog

另外请注意,您仍然可以访问一种SQL接口为这一点,但是,您应该始终确保joins|includes|eager_load您将查询的关联,即我加入上面的blog的方式。

Entry.joins(:blog).where("blogs.name = ?", "Beatles Blog") 

或使用命名参数

Entry.joins(:blog).where("blogs.name = :blog_name", blog_name: "Beatles Blog") 

希望我能帮助。

0

希望这会对你有所帮助。

Entry.where(blog__name: "Beatles Blog") 

假设blog__name是列名