我有一个Group
模型有很多Events
,我试图找到只有像Group.public_events
这样的公共事件的团体
这是我的小组:
class Group < ApplicationRecord
has_many :events, dependent: :destroy
end
这是我的活动:
class Event < ApplicationRecord
belongs_to :group
end
我想让scope :public_events
在赛事中找到.where(private: false)
,但是这应该是集体还是赛事呢?
会像这样参加比赛。
class Event < ApplicationRecord
belongs_to :group
scope :public_events, -> { where(private: false) }
end
你可以放入Event.rb
class Event < ApplicationRecord
belongs_to :group
scope :public_events, lambda { where('private = ?',false)}
end
你可以打电话给它
@public_events = Group.first.events.public_events
你可以用它来做。
scope :public_events, ->(parameter_if_any) {
where({ private: parameter_if_any||false } )
}