1

私は2つのモデルを持っています、CategoryそしてLineItemTypes

両方ともすでにたくさん存在しているので、それらを関連付ける必要があります。カテゴリごとに多くのLineItemTypesであること。

追加accepts_nested_attributes_for :line_item_typesしましたCategory

hidden_fieldフォームでを使用して、既存の関連付けられたリストを作成してみましたLineItemTypes

- form_for @category do |form|
  %ul#categorised
    - form.fields_for :line_item_types do |line_item_types|
      -categorised.each do |l|
        %li
          =l.description
          =line_item_types.hidden_field :category_id

  =form.submit

そのリストにアイテムを追加すると、そのカテゴリのLineItemTypeが見つからないというエラーが表示されます。accepts_nested_attributes_forは、関連付けが存在しない場合に関連付けを追加すると思いました。または、新しい関係を作成するのではなく、新しいレコードを「作成」して既存の関係を変更するためだけのものですか。

a.update_attributes({:line_item_types_attributes => [{:id => 2767}, {:id => LineItemType.find(2).id}]})
ActiveRecord::RecordNotFound: Couldn't find LineItemType with ID=2 for Category with ID=1

結果のフォームパラメータをトラバースして関連付けを作成するために何かを書く必要のないアイデアはありますか?または、これを達成するためのさらに簡単な方法はありますか?

4

3 に答える 3

2

私は、accepts_nested_attributes_forがurl_forのように機能するという結論に達しました... IDの存在により、関係が存在すると想定されます。レンダリングaccepts_nested_attributes_forは、私がやりたいことに適していません。

私はこれをbeforeフィルターで回避しました:

def find_line_item_types
  params[:category][:line_item_types] = LineItemType.find(params[:category][:line_item_types].collect { |a| a[0].to_i }) if params[:category] and params[:category][:line_item_types]
end
于 2010-11-03T14:12:48.157 に答える
0

いいえ、line_item_types属性でcategory_idを指定せずに、Categoryインスタンスからline_item_typesを作成できるはずです。

has_manyおよびbelongs_to宣言で:inverse_ofオプションを取得したことを確認する必要があります。

# in Category
has_many :line_item_types, :inverse_of => :category

# In LineItemType
belongs_to :category, :inverse_of => :line_item_types

それが役立つかどうか教えてください。

于 2010-11-02T14:46:09.450 に答える
0

さて、私は同じ問題を抱えていたので、ソースを検索し、accepts_nested_attributes_forこの動作を可能にするためにモンキーパッチを適用しました...

https://gist.github.com/2223565

たくさんのように見えますが、実際には数行を変更しただけです。

module ActiveRecord::NestedAttributes::ClassMethods
  def accepts_nested_attributes_for(*attr_names)
    # ...
    #options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only)
    options.assert_valid_keys(:allow_destroy, :reject_if, :limit, :update_only, :allow_existing)
    # ...
  end
end

と...

module ActiveRecord::NestedAttributes
  def assign_nested_attributes_for_collection_association(association_name, attributes_collection, assignment_opts = {})
    # ...
    #existing_records = if association.loaded?
    #  association.target
    #else
    #  attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact
    #  attribute_ids.empty? ? [] : association.scoped.where(association.klass.primary_key => attribute_ids)
    #end

    existing_records = if options[:allow_existing] or not association.loaded?
      attribute_ids = attributes_collection.map {|a| a['id'] || a[:id] }.compact
      scope = options[:allow_existing] ? association.target_scope : association.scoped
      scope.where(association.klass.primary_key => attribute_ids)
    else
      association.target
    end
    # ...
  end
end
于 2012-03-28T04:20:57.430 に答える