gpt4 book ai didi

ruby-on-rails - Rails 3 多态关联 : scopes on the belongs_to side that reference the association are not working

转载 作者:行者123 更新时间:2023-12-04 03:45:51 24 4
gpt4 key购买 nike

我有以下多态关联:

class Invite < ActiveRecord::Base
belongs_to :inviteable, :polymorphic => true

class Event < ActiveRecord::Base
has_many :invites, :as => :inviteable, :dependent => :destroy

class Team < ActiveRecord::Base
has_many :invites, :as => :inviteable, :dependent => :destroy

我正尝试在特定于事件和 future 事件的邀请上创建范围:

(in Invite.rb)
scope :to_events, where('inviteable_type = ?', "Event")
scope :to_future_events, lambda { self.to_events.joins("join events").where('events.starttime > ?', Time.now) }

to_events 范围工作正常; to_future_events 返回正确的邀请子集,但它返回每个邀请的许多副本。知道为什么吗?是否有更清洁/更 Rails 的方式来完成我想要完成的事情?

最佳答案

您必须为连接提供 ON 语句:

scope :to_future_events, lambda { self.to_events.joins("join events on events.id = invites.inviteable_id").where('events.starttime > ?', Time.now) }

否则它会将每个邀请记录与每个事件记录连接起来。

关于ruby-on-rails - Rails 3 多态关联 : scopes on the belongs_to side that reference the association are not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7365849/

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