rubyshoes
2012-Mar-31  17:11 UTC
Cannot set an attribute in my Join table .... do you think it matters which controller I use?
I tried out some code in my console and I am having difficulty
translating it for my view and controller so that it gives me the same
result.
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 the same result namely:
        id    contract_id    code_id   units_alloc
        91         64            54       80.00
But if I try to run this through my contracts_controller and view I get:
        id    contract_id    code_id   units_alloc
        1         1           1               ---I think this record comes from
the *@contract.codes.build*
        1         1                     80.00 ---I think this record comes from
the *@contract.codelines.build(:units_alloc => params[:units_alloc],:contract
=> @contract)*
Here are 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/create action of my app/controllers/contracts_controller.rb
  def new
    @contract = Contract.new
    @contract.codes.build
    @contract.codelines.build(:units_alloc => params[:units_alloc],:contract
=> @contract)
  end
  def create
     @contract = Contract.new(params[:contract])
     if @contract.save
       flash[:success] = "New Contract has been saved"
       redirect_to @contract
     else
       @title = "You have some errors"
       render ''new''
     end
   end
the partial for my view in app/views/contracts/_fields.html.haml
    = 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
Can you please have a look at this and give me some direction?
Thanks.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/bXaxeLJJyJsJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Phillip
2012-Apr-01  12:15 UTC
Re: Cannot set an attribute in my Join table .... do you think it matters which controller I use?
Something you think should be happening isn''t happening. I
don''t see
any debugging code in your files.
Are you using your log file to verify the parameters being passed in
from your form and review the queries being executed?
You might want to avoid mass assignment for security reasons and to
give more control.
For example, in your controller,
     :stuff = { :authnum    => params[:contract][:authnum],
                  :st_date      => params[:contract][:st_date],
                  :end_date    => params[:contract][:end_date] }
     logger.debug("------------------------ stuff #{stuff.inspect}")
     @contract = Contract.new(stuff)
On Mar 31, 1:11 pm, rubyshoes
<hlog...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I tried out some code in my console and I am having difficulty
> translating it for my view and controller so that it gives me the same
> result.
> 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 the same result namely:
>
>         id    contract_id    code_id   units_alloc
>         91         64            54       80.00
>
> But if I try to run this through my contracts_controller and view I get:
>
>         id    contract_id    code_id   units_alloc
>         1         1           1               ---I think this record comes
from the *...@contract.codes.build*
>         1         1                     80.00 ---I think this record comes
from the *...@contract.codelines.build(:units_alloc =>
params[:units_alloc],:contract => @contract)*
>
> Here are 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/create action of my app/controllers/contracts_controller.rb
>
>   def new
>     @contract = Contract.new
>     @contract.codes.build
>     @contract.codelines.build(:units_alloc =>
params[:units_alloc],:contract => @contract)
>   end
>
>   def create
>      @contract = Contract.new(params[:contract])
>      if @contract.save
>        flash[:success] = "New Contract has been saved"
>        redirect_to @contract
>      else
>        @title = "You have some errors"
>        render ''new''
>      end
>    end
>
> the partial for my view in app/views/contracts/_fields.html.haml
>
>     = 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
>
> Can you please have a look at this and give me some direction?
>
> Thanks.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Tom Tom
2012-Apr-06  22:14 UTC
Re: Cannot set an attribute in my Join table do you think it matters which controller I use?
I found a solution that resolved all my issues at this site, <a href="https://makandracards.com/makandra/1346-debug-nested-forms">debugging nested_forms</a> Thanks -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.