查询Rails中的条件

gmi*_*ile 2 ruby-on-rails

假设我们有两个表:"Items"和"Types".关系是:

item belongs_to type
type has_many items
Run Code Online (Sandbox Code Playgroud)

此外,Item表有一列,我们称之为"mark".什么是查询(如果可能的话,以Rails安全的方式)从中提取所有类型的类型表,它们在Items表中连接了带有"标记"的项目?

gio*_*ian 5

这个:

Type.find :all, :include => items, :conditions => ['items.mark = ?', somevalue]
Run Code Online (Sandbox Code Playgroud)

应该管用.

注意:您不应该使用Type类名称或:type属性,因为此名称可能会导致冲突.