我正在教孩子们如何在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的多元化或其他操纵会以其他方式扰乱这种情况吗?有更好的名词吗?
答案 0 :(得分: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
我不确定这段代码是否会运行(如果有的话,我会感到震惊),但这就是我用来命名的内容。