nil object in view when building objects on two different associations

Posted by Shako on Stack Overflow See other posts from Stack Overflow or by Shako
Published on 2010-02-08T07:35:51Z Indexed on 2010/06/16 12:42 UTC
Read the original article Hit count: 175

Filed under:

Hello all. I'm relatively new to Ruby on Rails so please don't mind my newbie level!

I have following models:

class Paintingdescription < ActiveRecord::Base
  belongs_to :paintings
  belongs_to :languages
end

class Paintingtitle < ActiveRecord::Base
  belongs_to :paintings
  belongs_to :languages
end

class Painting < ActiveRecord::Base
  has_many :paintingtitles, :dependent => :destroy
  has_many :paintingdescriptions, :dependent => :destroy  
  has_many :languages, :through => :paintingdescriptions
  has_many :languages, :through => :paintingtitles
end

class Language < ActiveRecord::Base
  has_many :paintingtitles, :dependent => :nullify
  has_many :paintingdescriptions, :dependent => :nullify
  has_many :paintings, :through => :paintingtitles
  has_many :paintings, :through => :paintingdescriptions  
end

In my painting new/edit view, I would like to show the painting details, together with its title and description in each of the languages, so I can store the translation of those field.

In order to build the languagetitle and languagedescription records for my painting and each of the languages, I wrote following code in the new method of my Paintings_controller.rb:

@temp_languages = @languages
@languages.size.times{@painting.paintingtitles.build}
@painting.paintingtitles.each do |paintingtitle|
  paintingtitle.language_id = @temp_languages[0].id
  @temp_languages.slice!(0)
end

@temp_languages = @languages
@languages.size.times{@painting.paintingdescriptions.build}
@painting.paintingdescriptions.each do |paintingdescription|
  paintingdescription.language_id = @temp_languages[0].id
  @temp_languages.slice!(0)
end

In form partial which I call in the new/edit view, I have

<% form_for @painting, :html => { :multipart => true} do |f| %>
...
  <% languages.each do |language| %>
    <p>
        <%= label language, language.name %>

        <% paintingtitle = @painting.paintingtitles[counter] %>
        <% new_or_existing = paintingtitle.new_record? ? 'new' : 'new' %>       

        <% prefix = "painting[#{new_or_existing}_title_attributes][]" %>
        <% fields_for prefix, paintingtitle do |paintingtitle_form| %>
            <%= paintingtitle_form.hidden_field :language_id%>
            <%= f.label :title %><br />
            <%= paintingtitle_form.text_field :title%>
        <% end %>      


        <% paintingdescription = @painting.paintingdescriptions[counter] %>
        <% new_or_existing = paintingdescription.new_record? ? 'new' : 'new' %>     

        <% prefix = "painting[#{new_or_existing}_title_attributes][]" %>
        <% fields_for prefix, paintingdescription do |paintingdescription_form| %>
            <%= paintingdescription_form.hidden_field :language_id%>
            <%= f.label :description %><br />
            <%= paintingdescription_form.text_field :description %>
        <% end %>                             

    </p>
  <% counter += 1 %>
  <% end %>  
...
<% end %>

But, when running the code, ruby encounters a nil object when evaluating paintingdescription.new_record?:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.new_record?

However, if I change the order in which I a) build the paintingtitles and painting descriptions in the paintings_controller new method and b) show the paintingtitles and painting descriptions in the form partial then I get the nil on the paintingtitles.new_record? call.

I always get the nil for the objects I build in second place. The ones I build first aren't nil in my view. Is it possible that I cannot build objects for 2 different associations at the same time? Or am I missing something else?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about ruby-on-rails