2013-05-22 31 views
0

獲取數據我有自我指涉模型稱爲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

,所以我可以訪問的關係所屬的組。

是我的設計,或者我在這裏忽略了什麼?

回答

0

我想,你的集團將擁有許多配置文件和貴集團要求的關係,你可以通過關係

0

我越來越接近的回答你的小組或者關係或曲線或輪廓。我傳遞一個塊HAS_MANY

has_many accepted_relationships... do 
    def assigned_group 
    #code 
    end 
end 

我還沒有得到相當,但我覺得這是我需要的路線。