首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails与Hash多个表连接

Rails与Hash多个表连接
EN

Stack Overflow用户
提问于 2016-08-15 17:56:50
回答 1查看 766关注 0票数 2

我正在尝试执行一个查询,它返回所有有动物的用户的名称,其中tipo是'Cachorro‘

所以我有三张桌子

代码语言:javascript
复制
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

到目前为止,我还没有弄清楚我应该如何在控制台中构造这个查询:

代码语言:javascript
复制
User.joins(:animals, :tipo).where(animals.tipo.tipo_animal: 'Cachorro')

不管用,知道该怎么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-15 18:07:35

在Ruby中,您不能使用简单的单词animals.tipo.tipo_animal。这对Ruby来说是没有意义的,因为这些不是在当前范围中定义的东西。

您需要给ActiveRecord一个包含该列名的字符串:

代码语言:javascript
复制
...where("animals.tipo.tipo_animal" => 'Cachorro')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38960332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档