我正在教孩子们如何在 Rails 中做一个特定的应用程序,它需要一个关于某人对别人负责的资源。例如,这里有 2 个模型:
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 的多元化或其他操作会以其他方式扰乱它吗?有更好的名词吗?