开发者

Searching through Table connections in Rails

开发者 https://www.devze.com 2023-03-28 06:13 出处:网络
Suppose I have two tables, one called zombies (id, name, gra开发者_运维知识库veyard), and one called weapons (id, name, strength, zombie_id).Weapons :belongs_to Zombies What\'s the easiest way to find

Suppose I have two tables, one called zombies (id, name, gra开发者_运维知识库veyard), and one called weapons (id, name, strength, zombie_id). Weapons :belongs_to Zombies What's the easiest way to find the weapon that has a zombie of name 'jack'?

Thanks!


Weapon.includes(:zombie).where("zombies.name = ?", "jack")

This will return a collection, so if you want just the first, append .first to that query.

So in conclusion: includes will do a join, but also keep around the table included so you can reference it in the where clause.

0

精彩评论

暂无评论...
验证码 换一张
取 消