Reputation: 1065
So I have the following:
class ComplexAssertion < ActiveRecord::Base
has_many :assertion_groups
has_many :assertions, :through => :assertion_group
class AssertionGroup < ActiveRecord::Base
belongs_to :complex_assertion
has_many :assertions, :dependent => :destroy, :autosave=>true
class Assertion < ActiveRecord::Base
belongs_to :assertion_group
And my form looks like the following:
<%= form_for(@complex_assertion) do |f| %>
<div class="childGroup1" style="padding:5px;">
<%= f.fields_for :assertion_groups do |assertion_group_fields| %>
<%= assertion_group_fields.fields_for :assertions do |assertion_fields| %>
Type: <%= assertion_fields.collection_select :assertion_type_id, AssertionType.all, :id, :name %>
Attribute: <%= assertion_fields.collection_select :attribute_name, Attribute.find_by_sql("select distinct a.name from attributes a "), :name, :name %>
<%= assertion_fields.label :operator_type_id %>: <%= assertion_fields.collection_select :operator_type_id, OperatorType.all, :id, :value %>
Value: <%= assertion_fields.text_field :value, :size=>'1' %>
<% end %>
<div id="innerOperator">
<!--OPERATOR BETWEEN EACH ASSERTION IN A GROUP-->
<%= assertion_group_fields.collection_select :logical_operator_type_id, LogicalOperatorType.all, :id, :value %>
</div>
<% end %>
</div>
<div id="createComplex" align="center">
<%= f.submit :value=>'Submit' %>
</div>
<% end %>
When I try to save, I get the following error:
AssertionGroup(#48180276) expected, got Array(#19330344)
Rails.root: C:/Eagle6/Assertions
Application Trace | Framework Trace | Full Trace
app/controllers/complex_assertions_controller.rb:43:in `new'
app/controllers/complex_assertions_controller.rb:43:in `create'
I've tried to make multiple changes to get this to work correctly, but I think I'm missing some very fundamental piece of logic here. Can anyone point me in the right direction? I was under the impression that if I got the Models done correctly and used the Form_builders, that Rails would handle the inserts and relationships.
Thanks!
Added controller code - very vanilla.
def create
@complex_assertion = ComplexAssertion.new(params[:complex_assertion])
respond_to do |format|
if @complex_assertion.save
format.html { redirect_to(@complex_assertion, :notice => 'Complex assertion was successfully created.') }
format.xml { render :xml => @complex_assertion, :status => :created, :location => @complex_assertion }
else
format.html { render :action => "new" }
format.xml { render :xml => @complex_assertion.errors, :status => :unprocessable_entity }
end
end
end
Upvotes: 0
Views: 588
Reputation: 25994
Your ComplexAssertion
class should look like this
class ComplexAssertion < ActiveRecord::Base
has_many :assertion_groups
has_many :assertions, :through => :assertion_group
accepts_nested_attributes_for :assertion_groups # <= add this
Your AssertionGroup
class should look like this
class AssertionGroup < ActiveRecord::Base
belongs_to :complex_assertion
has_many :assertions, :dependent => :destroy, :autosave=>true
accepts_nested_attributes_for :assertions # <= add this
You can find more about this at http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html ("Nested attributes examples", "One-to-many").
Upvotes: 2