2016-04-24 63 views
-1

我有這樣的問題。例如,我有3種型號:用戶,項目連接:如何訪問多對多的屬性

class User < ActiveRecord::Base 
    has_many :connections 
    has_many :tasks, :through => :connections 
end 
class Project < ActiveRecord::Base 
    has-many :connections 
    has_many :users, :through => :connections 
end 
class Connection < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :project 
    enum status: [:manager, :developer] 
end 

的問題是:我怎麼能訪問模型中的連接「狀態」屬性? 例如,我拿一個項目:

@project = Projects.last 

然後我得到這個項目的用戶,也是我很感興趣,他們在項目中發揮作用:

@project.users.each do |u| 
    u.name 
    u.connection.role #Here I get an error `undefined method `connection'` 
end 

當然u.role中將不起作用,因爲用戶沒有這種屬性「角色」。 我想顯示和編輯屬性「角色」 有什麼想法?

回答

0

我已經用這種方法解決了這個問題: user.connections.find_by_project_id(@project.id).role%>