【发布时间】:2013-11-12 20:00:05
【问题描述】:
我很好奇它是如何工作的......我在相同的两个模型之间有几个 has_many 关系。这适用于日历应用程序,因此可以邀请用户参加活动,用户可以参加活动,并且活动属于用户,以便查看谁创建了活动。
user.rb
class User < ActiveRecord::Base
has_many :invites, dependent: :destroy
has_many :events, through: :invites
has_many :events
has_many :attendances, -> { where cancel: false },
dependent: :destroy
has_many :events, -> { where "attendances.cancel" => false },
through: :attendances
event.rb
class Event < ActiveRecord::Base
has_many :invites, dependent: :destroy
has_many :users, through: :invites
belongs_to :user
has_many :attendances, -> { where cancel: false },
dependent: :destroy
has_many :users, -> { where "attendances.cancel" => false },
through: :attendances
我也有相应的连接表,在它们各自的模型中控制,attendance.rb 和 invite.rb。
所以...这按预期工作。用户在参加活动时有活动。我做了一些调整,并意识到这是检查列表中的最后一件事。所以,如果我将邀请移到底部,那么当我执行User.find(1).events 之类的操作时,就会检查到这一点。
有没有更好的方法来解决这个问题?我觉得这只是自找麻烦,不是吗?
【问题讨论】:
标签: ruby-on-rails ruby activerecord ruby-on-rails-4