帮助一个加盟的Rails 3 [英] Help with a Join in Rails 3

查看:116
本文介绍了帮助一个加盟的Rails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

class Event < ActiveRecord::Base
  has_many :action_items
end

class ActionItem < ActiveRecord::Base
  belongs_to :event
  belongs_to :action_item_type
end

class ActionItemType < ActiveRecord::Base
  has_many :action_items
end

和我想要做的是,对于一个给定的事件,找出所有有一个动作项目类型与富(例如)的名称的行动项目。因此,我认为SQL会去是这样的:

And what I want to do is, for a given event, find all the action items that have an action item type with a name of "foo" (for example). So I think the SQL would go something like this:

SELECT * FROM action_items a
INNER JOIN action_item_types t
ON a.action_item_type_id = t.id
WHERE a.event_id = 1
AND t.name = "foo"

谁能帮我翻译这成为一个很好的活动记录查询? (Rails的3 - 阿雷尔)

Can anybody help me translate this into a nice active record query? (Rails 3 - Arel)

谢谢!

推荐答案

嗯,我想我解决了它自己。这是我做

Well I think I solved it myself. Here's what I did

e = Event.find(1)
e.action_items.joins(:action_item_type).where("action_item_types.name = ?", "foo")

这篇关于帮助一个加盟的Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆