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
我沒有測試過高於這個特定的代碼,但這個概念對我來說很有意義......雖然responsible
和responsible for
是一個形容詞,介詞和分別,不是名詞因爲Rails中的資源應該是。有一個更好的方法嗎? Rails的多元化或其他操作會以其他方式混淆?有更好的名詞嗎?
您可能需要在'through'鍵後設置'source'。所以作爲一個例子,你應該有'has_many:responsible_for_people,通過:: responsible_fors,source:responsible_for'。 – Ashitaka 2013-02-14 23:33:44