gpt4 book ai didi

ruby-on-rails - 帮助加入 Rails 3

转载 作者:行者123 更新时间:2023-12-01 07:30:57 26 4
gpt4 key购买 nike

我有以下型号:

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

我想要做的是,对于给定的事件,找到所有具有名称为“foo”的操作项类型的操作项(例如)。所以我认为 SQL 会是这样的:
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 - Arel)

谢谢!

最佳答案

好吧,我想我自己解决了。这是我所做的

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

关于ruby-on-rails - 帮助加入 Rails 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4280315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com