Help with a Join in Rails 3

Posted by Adam Albrecht on Stack Overflow See other posts from Stack Overflow or by Adam Albrecht
Published on 2010-11-25T19:29:41Z Indexed on 2010/12/31 19:54 UTC
Read the original article Hit count: 364

I have the following models:

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

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"

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

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord