1

我在教孩子們如何在Rails中做一個特定的應用程序,它需要一個關於某人對其他人負責的資源。例如,這裏有2種型號:如何命名爲形容詞或介詞的Rails資源?

class User < ActiveRecord::Base 
    attr_accessible :name 
    has_many :responsible_fors, class_name: 'ResponsibleFor', foreign_key: 'responsible_by_ user_id' 
    has_many :responsible_for_people, through: :responsible_fors 
    has_many :responsible_bys, class_name: 'ResponsibleFor', foreign_key: 'responsible_for_user_id' 
    has_many :responsible_by_people, through: :responsible_bys 
end 
class ResponsibleFor < ActiveRecord::Base 
    attr_accessible :relationship, :responsible_by_id, :responsible_for_id 
    belongs_to :responsible_by, class_name: 'User' 
    belongs_to :responsible_for, class_name: 'User' 
end 

我沒有測試過高於這個特定的代碼,但這個概念對我來說很有意義......雖然responsibleresponsible for是一個形容詞,介詞和分別,不是名詞因爲Rails中的資源應該是。有一個更好的方法嗎? Rails的多元化或其他操作會以其他方式混淆?有更好的名詞嗎?

+0

您可能需要在'through'鍵後設置'source'。所以作爲一個例子,你應該有'has_many:responsible_for_people,通過:: responsible_fors,source:responsible_for'。 – Ashitaka 2013-02-14 23:33:44

回答

0

我不認爲你將不得不使用Rails的任何問題,並以複數:

1.9.3p385 :023 > "responsible_for".pluralize 
=> "responsible_fors" 
1.9.3p385 :024 > _.singularize 
=> "responsible_for" 

這就是說,我只想一起去:

class User < ActiveRecord::Base 
    has_many :responsibilities 
    has_many :stakeholders, through: :responsibilities 
    has_many :responsible_parties, through: responsibilities 
end 

class Responsibility < ActiveRecord::Base 
    attr_accessible :relationship, :stakeholder_user_id, :responsible_party_user_id 
    belongs_to :stakeholder 
    belongs_to :responsible_party 
end 

我不知道這段代碼運行(如果它真的會令我震驚),但這是我用來命名的。

相關問題