0

こんにちは私は次のような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ような関係があるのか​​理解できません。この場合、それが私が使用することになっているものであるかどうか、誰かが私にアイデアを教えてもらえますか?

4

1 に答える 1

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_protectedの代わりにattr_accessibleを使用することをお勧めします

Fooに多くのバーがあり、バーが多くのFooに属している場合、次のようになります。

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にはたくさんのヘルプがあります

また、SOには1兆の例があります。

お役に立てば幸い

于 2012-11-21T23:04:59.827 に答える