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 7758513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:22:52+00:00 2026-06-01T13:22:52+00:00

I am having real difficulty getting my head around the issue of setting the

  • 0

I am having real difficulty getting my head around the issue of setting the attribute in my join model?

My models:

  class Contract < AR::Base 
    has_many :codelines
    has_many :codes, :through => :codelines

    accepts_nested_attributes_for :codes

    attr_accessible :codes_attributes, :codes; :authnum, :st_date, :end_date
  end

  class Codeline < AR::Base
    belongs_to :contract
    belongs_to :code
    units_alloc ...... this is the attribute I would like to set
  end

  class Code < AR::Base
    has_many :codelines
    has_many :contracts, :through => :codelines
  end

The new action of my app/controllers/contracts_controller.rb

  def new
    @contract = Contract.new
    @contract.codes.build
  end

the partial for my view in app/views/contracts/_fields.html.haml

  <fieldset><legend>Enter Billing Code Details</legend>
  = f.fields_for :codes do |ff|
     .field
        = ff.label :name, "Code Name"
        %br/
        = ff.text_field :code_name
     .field
     .
     .
   = f.fields_for :codelines do |ff|
     .field
       = ff.label :name, "Units Alloc"
       %br/
       = ff.text_field :units_alloc, :precision => 6, :scale => 2, :size => 10
  </fieldset>

From reading Rails Guides and watching railscasts #196 and #197 and researching nested attributes on the internet I came to the understanding that the @contract.codes.build line in the new action of my app/controllers/contracts_controller.rb not only built the code object but that it also built the codelines object. If I leave my new action of my app/controllers/contracts_controller.rb as above that is exactly what happens. My codelines table is populated as follows:

    id       contract_id       code_id       units_alloc
     1             1                1                  .... @contract.codes.build

But if you look at my view I would actually like to set units_alloc and this line @contract.codes.build does not give me access to units_alloc since it is in the codeline model. I added @contract.codelines.build to the new action of my app/controllers/contracts_controller.rb and now the units_alloc shows in my view and I am able to set it. But now I have two rows in my codelines table. One resulting from the @contract.codes.build where both the code object and codeline object are built and the second row is as a result of the @contract.codelines.build and my codelines table is as follows:

    id       contract_id       code_id       units_alloc
     1             1                1                  .... @contract.codes.build
     2             1                             80.00 .... @contract.codelines.build

Should’nt I be able to get access to units_alloc through the first build given by @contract.codes.build, where both the code object and the codeline object is built?

Does anyone know if my understanding of this issue is correct or could you point me to a resource where all will be revealed?

Just as an after thought I built the code object through codelines and I got the same result.

Thanks for any suggestions.

Update
I can get my codelines table to have only one record with all the associated id’s set as follows:

My console:

    @contract = Contract.new(authnum: "900700", st_date: "2012-01-01",  end_date:
    "2012-30-06")

    @contract.save

    @code = Code.new(code_name: "S-5463", status: "Active",   description: 
    "This and That")

    @code.save

    @codeline = @code.codelines.build(:units_alloc => "80.00",  :contract => @contract)

    @codeline.save

    @codeline
    => #<Codeline id: 91, contract_id: 64, code_id: 54, units_alloc: 80.00>

Using pgadmin3 I check my codelines table and I get just one record, namely:

    id    contract_id    code_id   units_alloc
    91         64            54       80.00

Now the test is to get my contracts_controller new/create action to do the same.

  • 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-06-01T13:22:53+00:00Added an answer on June 1, 2026 at 1:22 pm

    I found a solution at this site, debugging nested_forms.

    It is the ninth bullet down where you read that if you are working with a has_many :through association then you need to base your nested_form on the join model or something close to that.

    I quickly ran a test by refactoring some code here and there and I now have a workable nested form that serves up the correct params to the controller which in turns processes it correctly and my codelines table now has just one record.

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

Sidebar

Related Questions

I'm having real trouble getting my head around this issue. As the title suggests,
I'm having real difficulty getting the eclipselink.join-fetch hint to work in glassfish. I have
I'm having real difficulty getting access to a variable that is an attribute of
I'm having real problems getting PDO_MYSQL working. I started by just trying to install
I'm having a real difficult time getting this code to work. I'm trying to
I'm having real difficulty with this but I'm no javascript expert. All I want
I am having some real difficulty attempting to solve a JQuery $.each() iteration This
I am having a real difficulty. I have a number of images with a
I'm having real problems getting a ProgressDialog up and running. My code: ProgressDialog dialog;
I'm having real difficulty pulling all atttachments including the image... The thing is, I

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.