Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5986191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:38:14+00:00 2026-05-22T22:38:14+00:00

The models I’m working with look like this: class ComplexAssertion < ActiveRecord::Base has_many :expression_groups

  • 0

The models I’m working with look like this:

class ComplexAssertion < ActiveRecord::Base
   has_many :expression_groups
   has_many :expressions, :through => :expression_group
   accepts_nested_attributes_for :expression_groups, :allow_destroy=>true
end

class ExpressionGroup < ActiveRecord::Base
   belongs_to :complex_assertion
   has_many :expressions
   accepts_nested_attributes_for :expressions, :allow_destroy=>true
end

class Expression < ActiveRecord::Base
   belongs_to :expression_group
end

My form looks like the following:

<%= form_for(@complex_assertion) do |f| %>
<div id="mainAssertionGroup" style="border:1px; border-style:solid; width:1000px; padding:5px">
  <div class="field">
    <%= f.label :title %>: <%= f.text_field :title, :size=>'10' %>
    <%= f.label :description %>: <%= f.text_field :description, :size=>'25' %>
    <%= f.label :scope %>: <%= f.text_field :scope, :size=>'1' %>
    Test
    Category: <%= collection_select(:complex_assertion, :assertion_category_id, AssertionCategory.all, :id, :name, {:include_blank=>"UNCATEGORIZED"}) %>
  </div>
  <div id="initialGroup" style="border:1px; margin-left:10px; margin-top:10px; border-style:solid; width:850px;">
    <div class="childGroup1" style="padding:5px;">
      <%= f.fields_for :expression_groups do |eg| %>
          <%= eg.fields_for :expressions do |e| %>
              Type: <%= e.collection_select :assertion_type_id, AssertionType.all, :id, :name %>
              Attribute: <%= e.collection_select :attribute_name, Attribute.find_by_sql("select distinct a.name from attributes a "), :name, :name %>
              <%= e.label :operator_type_id %>
              : <%= e.collection_select :operator_type_id, OperatorType.all, :id, :value %>
              Value: <%= e.text_field :value, :size=>'1' %>
          <% end %>
          <div id="innerOperator">
            <%= eg.collection_select :logical_operator_type_id, LogicalOperatorType.all, :id, :value %>
          </div>
      <% end %>
    </div>
  </div>
</div>
<div id="createComplex" align="center">
  <%= f.submit :value=>'Submit' %>
</div>
<% end %>

And my controller looks like:

def new_complex_assertion
  @complex_assertion = ComplexAssertion.new
end

When I load the page, I only the ComplexAssertion portion of the form and get nothing back for the ExpressionGroups or the Expressions. It’s as if there isn’t anything available. But, if you see my controller, I did a ComplexAssertion.new which I though would create the dependent objects automagically; I assume I’m incorrect?

I’m debugging through RubyMine and when I evaluate the ComplexAssertion.new, I only see 5 attributes, the five that are defined for only that object, none of the relational objects. What am I doing incorrectly?

EDIT
Looks like if I do the following:

@complex_assertion = ComplexAssertion.new
@complex_assertion.expression_groups.build
@complex_assertion.expressions.build

And change my form to use:

<%= f.fields_for :expressions do |e| %>

instead of eg.fields_for, it shows the forms.

This DOES NOT give me the correct nesting. I thought I should be able to do:

@complex_assertion.expression_groups.expressions.build

but it tells me that expressions is an undefined method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:38:15+00:00Added an answer on May 22, 2026 at 10:38 pm

    Yes, you have to explicitly instantiate the associated objects. It is not done for you.

    @complex_assertion.expression_groups.expressions.build
    

    Will not work because expression_groups is an array and not an individual expression group. So, after you create the expressions_groups do the following:

    @complex_assertion.expressions_groups.each do |group|
      group.expressions.build
    end
    

    Also, you could replace the 2nd line with the following as well to create multiple expressions

    2.times do { group.expressions.build }
    

    As for using fields_for with nested models, make your code in the form look like this:

    <%= f.fields_for :expression_groups, @complex_assertions.expression groups do |eg| %>
      <%= eg.fields_for :expressions, eg.object.expressions do |e| %>
    

    I will try to explain what is going on. The :expressions_groups is telling fields_for what class of object it is going to render fields for, and the second part I added is telling fields_for where to find the object(s) to render fields for. If we are passing in an array, which we are in this case, it will automatically iterate over the array. On each iteration, it puts the current model object we are working with into a variable called object which is stored in the form builder instance returned by fields_for. So we use this to tell the second fields_for where to find the expression model objects it needed. This means eg.object points to an expression_group model object.

    I hope this helps and makes sense. Also, I have not tested anything and am only pointing out what looks out of place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
Situation # Models class User < ActiveRecord::Base has_many :items end class Items < ActiveRecord::Base
I have a models A and B , that are like this: class A(models.Model):
my models class Auction belongs_to :item belongs_to :user, :foreign_key => :current_winner_id has_many :auction_bids end
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
Assume the following: models.py class Entry(models.Model): title = models.CharField(max_length=50) slug = models.CharField(max_length=50, unique=True) body
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
I have two models, Article and Post that both inherit from a base model
Models (disregard typos / minor syntax issues. It's just pseudo-code): class SecretModel(models.Model): some_unique_field =
In models and controllers, we often use Rails macros like before_validation , skip_before_filter on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.