[英]has_many relationship rails 3
嗨,我有以下三个表格:
class Workitem < ActiveRecord::Base
has_many :effort
attr_protected
end
class Effort < ActiveRecord::Base
attr_protected
belongs_to :workitem
belongs_to :person
end
class Person < ActiveRecord::Base
attr_accessible :given_name, :mgrid, :surname, :id
has_many :effort
end
这个想法是通过努力表来跟踪一个人在特定工作项目上花费了多少天。 有人可以验证我的关系是否正确吗? 但这似乎不起作用。 我在这里想念什么吗? 另外,我无法理解has_many :through
某种关联。 有人可以给我一个想法,如果这是我在这种情况下应该使用的吗?
通常,您会将孩子作为复数对象:
class Workitem < ActiveRecord::Base
has_many :efforts
attr_protected
end
class Person < ActiveRecord::Base
attr_accessible :given_name, :mgrid, :surname, :id
has_many :efforts
end
而且我建议使用attr_accessible而不是attr_protected
如果一个Foo有很多Bar,而Bar属于许多Foos,则它可能看起来像这样:
class Foo < ActiveRecord::Base
has_many :foo_bar
has_many :bars, through => :foo_bar
end
class Bar < ActiveRecord::Base
has_many :foo_bar
has_many :foos, through => :foo_bar
end
class FooBar
belongs_to :foo
belongs_to :bar
end
反正就是这样。 这里有很多关于Railcast的帮助
此外,SO上有数万亿个示例。
希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.