Ruby on rails 如何为rails 5中的多个关系创建范围?

Ruby on rails 如何为rails 5中的多个关系创建范围?,ruby-on-rails,activerecord,ruby-on-rails-5,Ruby On Rails,Activerecord,Ruby On Rails 5,我有一个包含许多事件的组模型,我正试图通过类似Group.public\u Events的方式找到只包含公共事件的组 这是我的小组: class Group < ApplicationRecord has_many :events, dependent: :destroy end 以下是我的活动: class Event < ApplicationRecord belongs_to :group end 我想做一个范围:public_events to find.wher

我有一个包含许多事件的组模型,我正试图通过类似Group.public\u Events的方式找到只包含公共事件的组

这是我的小组:

class Group < ApplicationRecord
  has_many :events, dependent: :destroy
end
以下是我的活动:

class Event < ApplicationRecord
  belongs_to :group
end

我想做一个范围:public_events to find.whereprivate:false on Event,但这应该发生在组或事件上吗

您可以在Event.rb中输入

class Event < ApplicationRecord
  belongs_to :group
  scope :public_events, lambda { where('private = ?',false)}
end

您可以在Event.rb中输入

class Event < ApplicationRecord
  belongs_to :group
  scope :public_events, lambda { where('private = ?',false)}
end

在这样的事件中我会去的

class Event < ApplicationRecord
  belongs_to :group
  scope :public_events, -> { where(private: false) }
end

在这样的事件中我会去的

class Event < ApplicationRecord
  belongs_to :group
  scope :public_events, -> { where(private: false) }
end
你可以用它来做这个

scope :public_events, ->(parameter_if_any) {
  where({ private: parameter_if_any||false } )
}
你可以用它来做这个

scope :public_events, ->(parameter_if_any) {
  where({ private: parameter_if_any||false } )
}

如果我这样做,我会得到一个未定义的方法错误。我做错了什么?Thxah很抱歉我忘了提到.events,我刚刚更正了上面@public\u events=Group.first.events.public\u events如果我这样做,我会得到一个未定义的方法错误。我做错了什么?Thxah抱歉,我忘了提到.events,我刚刚更正了上面的@public\u events=Group.first.events.public\u events您是否将其称为Group.first.public\u events?因为我有一个命名者。您可以将其称为Group.public\u events将其称为Group.public\u events是否将其称为Group.first.public\u events?因为我有一个命名者。您可以将其称为Group.public\u事件将其称为Group.public\u事件