简体   繁体   English

has_many关系栏3

[英]has_many relationship rails 3

Hi I have three tables like the following: 嗨,我有以下三个表格:

    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

The idea is to keep track of how many days a person has spent on a particular work item through efforts table. 这个想法是通过努力表来跟踪一个人在特定工作项目上花费了多少天。 Could somebody verify if my relationships are correct? 有人可以验证我的关系是否正确吗? But this doesn't seem to work. 但这似乎不起作用。 Am I missing something here? 我在这里想念什么吗? Also, I can't understand the has_many :through kind of associations. 另外,我无法理解has_many :through某种关联。 Can somebody please give me an idea if that is what I'm supposed to use in this case? 有人可以给我一个想法,如果这是我在这种情况下应该使用的吗?

You would usually have the child as a plural object: 通常,您会将孩子作为复数对象:

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

And I'd recommend using attr_accessible instead of attr_protected 而且我建议使用attr_accessible而不是attr_protected

If a Foo had many Bars and the Bars belonged to many Foos, it might look like this: 如果一个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

Something like that anyway. 反正就是这样。 There's a load of help on Railcasts here 这里有很多关于Railcast的帮助

Also, there's a trillion examples on SO. 此外,SO上有数万亿个示例。

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM