accepts_nested_attributes_for with has_many => :through Options

后端 未结 7 1944
陌清茗
陌清茗 2020-12-08 10:10

I have two models, links and tags, associated through a third, link_tags. The following code is in my Link model.

Associations:

class Link < Acti         


        
7条回答
  •  感情败类
    2020-12-08 10:47

    In order for this to work, you need to pass in the right params hash:

    params = {
      :link => {
        :tags_attributes => [
          {:tag_one_attr => ...}, {:tag_two_attr => ...}
        ],
        :link_attr => ...
      }
    }
    

    And your controller will look like:

    def create
      @link = Link.create(params[:link]) # this will automatically build the rest for your
    end
    

提交回复
热议问题