
Untitled
By: a guest on
Jul 18th, 2012 | syntax:
None | size: 0.75 KB | hits: 6 | expires: Never
accepts_nested_attributes_for alias?
class Topic < ActiveRecord::Base
has_many :choices, :dependent => :destroy
accepts_nested_attributes_for :choices
attr_accessible :title, :choices
end
ActiveRecord::AssociationTypeMismatch (Choice(#70365943501680) expected,
got ActiveSupport::HashWithIndifferentAccess(#70365951899600)):
def create
choices = params[:topic].delete(:choices)
@topic = Topic.new(params[:topic])
if choices
choices.each do |choice|
@topic.choices.build(choice)
end
end
if @topic.save
render json: @topic, status: :created, location: @topic
else
render json: @topic.errors, status: :unprocessable_entity
end
end
def choices=(params)
self.choices_attributes = params
end