您好我有三张表如下:
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
种关联。有人可以告诉我这是否是我应该在这种情况下使用的?
答案 0 :(得分:1)
您通常会将孩子视为复数对象:
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有很多Bars并且Bars属于许多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
无论如何都是这样的。 Railcasts here
有很多帮助此外,SO上有数万个例子。
希望有所帮助