Ruby on rails 使用活动记录查找包含一个联接模型但不包含另一个联接模型的所有记录
我的rails应用程序中有以下模式Ruby on rails 使用活动记录查找包含一个联接模型但不包含另一个联接模型的所有记录,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我的rails应用程序中有以下模式 class Campaign < ApplicationRecord has_many :businesses has_many :clients, through: :businesses end class Business < ApplicationRecord has_many :clients belongs_to :campaign end class Client < ApplicationRecord
class Campaign < ApplicationRecord
has_many :businesses
has_many :clients, through: :businesses
end
class Business < ApplicationRecord
has_many :clients
belongs_to :campaign
end
class Client < ApplicationRecord
has_many :events
belongs_to :business
end
class Event < ApplicationRecord
belongs_to :client
enum category: {
:processed,
:delivered,
:opened
}
end
但这行不通
此查询将运行在一些相当大的表上,因此不允许进行快速加载。尝试这些查询,因为枚举通常无法与基本字符串进行比较
c = Campaign.first
c.clients.joins(:events).where.not("events.category = (?)", Event.category[:opened] )
或者只是
c = Campaign.first
c.clients.joins(:events).where("events.category = (?)",[ Event.category[:processed], Event.category[:delivered]] )
更多信息可以找到
您也可以看到谢谢,但这仍然没有帮助。我已经在查询中使用了原始枚举值,问题仍然是一样的。
c = Campaign.first
c.clients.joins(:events).where("events.category = (?)",[ Event.category[:processed], Event.category[:delivered]] )