獲取數據我有自我指涉模型稱爲Profile
即通過Relationship
模型連接。從連接表
class Profile < ActiveRecord::Base
belongs_to :user
has_many :accepted_relationships, class_name: 'Relationship', foreign_key: 'responder_id'
has_many :followers, through: :accepted_relationships, source: 'initiator'
has_many :initiated_relationships, class_name: 'Relationship', foreign_key: 'initiator_id'
has_many :followed_profiles, through: :initiated_relationships, source: 'responder'
has_many :groups
end
class Relationship < ActiveRecord::Base
belongs_to :responder, class_name: 'Profile', foreign_key: 'responder_id'
belongs_to :initiator, class_name: 'Profile', foreign_key: 'initiator_id'
belongs_to :group
end
class Group < ActiveRecord::Base
belongs_to :profile
has_many :relationships
attr_accessible :name
end
問題是我不知道如何訪問連接模型上的數據。如果我做了類似的事情;
user.profiles[1].followers[1]
它會給我我想要的配置文件。我也想有類似的東西;
user.profiles[1].followers[1].assigned_group
,所以我可以訪問的關係所屬的組。
是我的設計,或者我在這裏忽略了什麼?