我是 Rails 的新手,有一个关于为has_many :through
关系设置条件的问题。我想在模型中建立:joined_groups
关系。FacebookUser
join_groups 将是一个通过 GroupInvite 表满足的条件where(accepted: true)
。但是我不知道如何使用 Rails 来实现这一点,或者这是否是正确的方法。我有以下型号...
组.rb
class Group < ActiveRecord::Base
attr_accessible :description, :name
...
#Relationships
has_many :group_invites, class_name: 'GroupInvite'
has_many :facebook_users, class_name: 'FacebookUser', :through => :group_invites, :source => :facebook_user
end
GroupInvite.rb
class GroupInvite < ActiveRecord::Base
attr_accessible :accepted, :facebook_user_uid, :group_id, :admin
...
#Relationships
belongs_to :group
belongs_to :facebook_user, :foreign_key => :facebook_user_uid, :primary_key => :facebook_user_uid
#Scopes
scope :pending, where(accepted: nil)
scope :declined, where(accepted: false)
scope :joined, where(accepted: true)
end
Facebook的用户
class FacebookUser < ActiveRecord::Base
#Attributes
attr_accessible :first_name, :gender, :last_name, :uid
...
has_many :group_invites, class_name: 'GroupInvite', :primary_key => :uid, :foreign_key => :facebook_user_uid
has_many :groups, class_name: 'Group', :through => :group_invites, :source => :group
...
end
任何帮助将不胜感激!