Reputation: 473
Rails 4: I want to create Person with tags
person = Person.new(:name=>Jon', :tags_attributes=>[{:id=>'15', :name=>'some_tag'}])
My Person model:
class Person < ActiveRecord::Base
validates :name, presence: true
belongs_to :user
has_many :organizations, through: :people_organizations
has_and_belongs_to_many :tags, join_table: :people_tags
has_many :phones, as: :phoneable, :dependent => :destroy
has_many :emails, as: :emaileable, :dependent => :destroy
has_many :networks, as: :networkable, :dependent => :destroy
has_many :messengers, as: :messengerable, :dependent => :destroy
accepts_nested_attributes_for :phones, :emails, :networks, :messengers, allow_destroy: true
accepts_nested_attributes_for :tags, reject_if: :all_blank
end
My PeopleController:
def create
@person = Person.new(person_params)
respond_to do |format|
if @person.save(validate: false)
format.json { render action: 'show', status: :created }
else
format.json { render json: @person.errors, status: :unprocessable_entity }
end
end
end
def person_params
params.require(:person).permit(:id, :name, :born, :description,
phones_attributes:[:id, :number, :_destroy],
emails_attributes:[:id, :email, :_destroy]
networks_attributes:[:id, :name, :_destroy],
messengers_attributes:[:id, :identifier, :_destroy],
tags_attributes:[:id, :name, :_destroy]
)
end
When i create new person, i have error
p = Person.new(:name=>'Jon', :tags_attributes=>[{:id=>'15', :name=>'tag'}])
Couldn't find Tag with ID=15 for Person with ID=
Please tell me what to do to keep the model
Upvotes: 10
Views: 2751
Reputation: 69
You should create the nested fields through of people_tags
relationship, for example something like that
= form_for(@person) do |f|
= f.text_field :name
= f.simple_fields_for :people_tags do |people_tag_builder|
people_tag_builder.hidden_field :tag_id 15
= people_tag_builder.simple_fields_for :tags, user do |tag_builder|
= tag_builder.id 15
= tag_builder.text_field :name, value: 'tag'
the people_tag_builder.hidden_field :tag_id 15
is mandatory because it the middle table needs either person_id
or tag_id
how you are creating a person, you must send a tag object or create it or both
Upvotes: 0
Reputation: 1784
I got the same problem. I think that rails just don't support creating a new record with an existing nested record. I found no solution for this situation at all. So Try to filter tags_attributes from person_params and then use tag_ids like this:
tag_ids = params[:person][:tags_attributes].map { |tag| tag[:id] }
@person = Person.new(person_params.merge({ tag_ids: tag_ids })
Upvotes: 3