Ruby on rails 多个在相同的2个模型之间有多个/属于

Ruby on rails 多个在相同的2个模型之间有多个/属于,ruby-on-rails,ruby,activerecord,ruby-on-rails-4,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 4,我很好奇这是怎么回事。。。同一两个模型之间有好几种关系。这是用于日历应用程序的,因此用户可以被邀请参加活动,用户可以参加活动,并且活动属于用户,以便查看是谁创建了活动 user.rb class User < ActiveRecord::Base has_many :invites, dependent: :destroy has_many :events, through: :invites has_many :events has_many :attendances

我很好奇这是怎么回事。。。同一两个模型之间有好几种关系。这是用于日历应用程序的,因此用户可以被邀请参加活动,用户可以参加活动,并且活动属于用户,以便查看是谁创建了活动

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
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
class用户{取消:错误},
依赖::销毁
有很多:事件,->{where“出席。取消”=>false},
通过……出席
event.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
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
class事件{取消:错误},
依赖::销毁
有很多:用户,->{where“出席。取消”=>false},
通过……出席
我还拥有相应的联接表,在各自的模型中进行控制,
attention.rb
invite.rb

所以。。。这正如预期的那样有效。用户在参加活动时有活动。我做了一些调整,意识到这是列表中最后一个被检查的东西。因此,如果我将邀请移动到底部,那么当我执行类似于
User.find(1).events
的操作时,就会检查邀请


有没有更好的办法?我觉得这只是自找麻烦,不是吗?

当我以前这么做时,我只是将关系的名称更改为更独特的名称,然后通过
类名称告诉
有很多
要看什么类。您可能还必须在这些更改的关系上添加
外键
参数,以便SQL知道要匹配哪个键

基本思路如下:

user.rb

class User < ActiveRecord::Base
  has_many :invites, dependent: :destroy
  has_many :invited_events, through: :invites, source: "Event"

  has_many :events # these are events "owned" by the user

  has_many :attendances, -> { where cancel: false }, dependent: :destroy
  has_many :attended_events, -> { where "attendances.cancel" => false }, through: :attendances, source: "Event"
end
class用户{where cancel:false},依赖::destroy
是否有很多:参加了活动,->{where“attents.cancel”=>false},通过::attents,来源:“Event”
结束
event.rb

class Event < ActiveRecord::Base
  has_many :invites, dependent: :destroy
  has_many :invitees, through: :invites, source: "User"

  belongs_to :user # this is the owner of the event

  has_many :attendances, -> { where cancel: false }, dependent: :destroy
  has_many :attendees, -> { where "attendances.cancel" => false }, through: :attendances, source: "User"
end
class事件{where cancel:false},依赖::destroy
有很多:与会者,->{where“出席。取消”=>false},通过::出席,来源:“用户”
结束

我正在使用Rails 4。。。它向我大喊一些东西,并推荐
来源:
。我用
source::Event
替换了
class\u名称:“事件”
,一切正常。听起来对吗?