2011-01-07 14 views
4

とRailsのActiveRecordの中アソシエイト、およびデ関連付ける関連レコードは(記事にhas_manyの著者を言うことができます。)私が作成することができますどのように、私は2つのモデル間の典型的にhas_many関係を持っているaccepts_nested_attributes_for

私の記事のフォームには、ユーザーをすることができます:

  1. 著者との関連付けを削除する
  2. 、記事に関連付ける既存の著者を選択し、新しい作家を作成し、記事に関連付ける、
  3. (著者レコードを削除せず。)

私はaccepts_nested_attributes_forを使用しています。これは#1を完全に処理します。しかし、まだaccepts_nested_attributes_forを使用しながら#2と#3を実装する最良の方法を見つけることはまだありません。

これは実際にはすべてRails 3.0.0で動作していました。これまで見たことのない著者IDが与えられた場合、ActiveRecordは自動的に新しい関連付けを作成します。しかし、私は誤ってRails 3.0.1で修正されたセキュリティバグを悪用していることが判明しました。

私はさまざまなアプローチを試しましたが、何も完全に機能せず、この場合のベストプラクティスについて多くの情報を見つけることができません。

アドバイスはありがたいです。

おかげで、

ラッセル。

+0

あなたは結合テーブル(HABTM)を使用していますか?私は著者も多くの記事を持つことができると思います。あなたのフォームから入って来るparamsをそれぞれのケースに含めると役立つでしょう。 – aceofspades

答えて

1

これを見て:http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes

そのレール2.3のために、しかし、構文のほとんどはrails3と同じである...それはあなたが探してすべてのものに言及している。..

+0

ありがとうございますが、Ryanの投稿ではaccepts_nested_attributes_forがどのように動作するかについて説明しています。 #2(著者と関連付ける既存の著者を選択する)または#3(著者レコードを削除せずに著者との関連付けを削除する)のいずれかを説明していません。 –

+0

1つの考え:have_and_belongs_to_manyまたはhas_many:throughの関係、 1人の著者が複数の記事を持つことができ、1つの記事が複数の著者を持つことができるためです。通常のhas_many関係は正しい関係スキーマではありません。 – Lichtamberg

2

あなたはおそらく使用する必要があると仮定すると、結合テーブルこれをやってみる:

完全性については
class Article < ActiveRecord::Base 
    has_many :article_authors 
    accepts_nested_attributes_for :article_authors, allow_delete: true 
end 

class Author < ActiveRecord::Base 
    has_many :article_authors 
end 

class ArticleAuthor < ActiveRecord::Base 
    belongs_to :article 
    belongs_to :author 
    accepts.nested_attributes_for :author 
end 


# PUT /articles/:id 
params = { 
    id: 10, 
    article_authors_attributes: { 
    [ 
     # Case 1, create and associate new author, since no ID is provided 
     { 
     # A new ArticleAuthor row will be created since no ID is supplied 
     author_attributes: { 
      # A new Author will be created since no ID is supplied 
      name: "New Author" 
     } 
     } 

    ], 
    [ 
     # Case 2, associate Author#100 
     { 
     # A new ArticleAuthor row will be created since no ID is supplied 
     author_attributes: { 
      # Referencing the existing Author#100 
      id: 100 
     } 
     } 
    ], 
    [ 
     # Case 3, delete ArticleAuthor#101 
     # Note that in this case you must provide the ID to the join table to delete 
     { 
     id: 1000, 
     _destroy: 1 
     } 
    ] 
    } 
} 
+0

興味深いですが、別のモデルに 'belongs_to'するモデルから作成/関連付けを行う場合、指定されたOPと同じように、多対多の関係はありません:記事あたり1人の著者.. –

+0

コメントに従ってください。著者にはまだ多くの記事があり、その逆もあります。 belongs_toで明示的にjoinテーブルを宣言しても、それは変更されません。 – aceofspades

+0

ええ、しかし、あなたの解決策ではなく、1つではなく多くのものがあります。 'has_one:article_authors'はそれを行うかもしれません.. –

1

、私は今これをやっている方法はこれです:

class Article < ActiveRecord::Base 
    belongs_to :author, validate: false 
    accepts_nested_attributes_for :author 

    # This is called automatically when we save the article 
    def autosave_associated_records_for_author 
    if author.try(:name) 
     self.author = Author.find_or_create_by_name(author.name) 
    else 
     self.author = nil # Remove the association if we send an empty text field 
    end 
    end 

end 

class Author < ActiveRecord::Base 
    has_many :articles 
end 

私はそれに関連したモデル(著)を検証する方法を発見していないのです検証... ..

関連する問題