我正在尝试执行一个查询,它返回所有有动物的用户的名称,其中tipo是'Cachorro‘
所以我有三张桌子
User
has_many :animals
attribute sample
t.string :name
Animal
belongs_to :user
belongs_to :animals
attribute samples
user_id
tipo_id
Tipo
has_many :animals
attribute sample
tipo_animal到目前为止,我还没有弄清楚我应该如何在控制台中构造这个查询:
User.joins(:animals, :tipo).where(animals.tipo.tipo_animal: 'Cachorro')不管用,知道该怎么做吗?
发布于 2016-08-15 18:07:35
在Ruby中,您不能使用简单的单词animals.tipo.tipo_animal。这对Ruby来说是没有意义的,因为这些不是在当前范围中定义的东西。
您需要给ActiveRecord一个包含该列名的字符串:
...where("animals.tipo.tipo_animal" => 'Cachorro')https://stackoverflow.com/questions/38960332
复制相似问题