Search Results

Search found 2 results on 1 pages for 'shako'.

Page 1/1 | 1 

  • nil object in view when building objects on two different associations

    - by Shako
    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!

    Read the article

  • Joining the same model twice in a clean way, but making the code reusable

    - by Shako
    I have a model Painting which has a Paintingtitle in each language and a Paintingdescription in each language: class Painting < ActiveRecord::Base has_many :paintingtitles, :dependent => :destroy has_many :paintingdescriptions, :dependent => :destroy end class Paintingtitle < ActiveRecord::Base belongs_to :painting belongs_to :language end class Paintingdescription < ActiveRecord::Base belongs_to :painting belongs_to :language end class Language < ActiveRecord::Base has_many :paintingtitles, :dependent => :nullify has_many :paintingdescriptions, :dependent => :nullify has_many :paintings, :through => :paintingtitles end As you might notice, I reference the Language model from my Painting model via both the Paintingtitle model and Paintingdescription model. This works for me when getting a list of paintings with their title and description in a specific language: cond = {"paintingdescription_languages.code" => language_code, "paintingtitle_languages.code" => language_code} cond['paintings.publish'] = 1 unless admin paginate( :all, :select => ["paintings.id, paintings.publish, paintings.photo_file_name, paintingtitles.title, paintingdescriptions.description"], :joins => " INNER JOIN paintingdescriptions ON (paintings.id = paintingdescriptions.painting_id) INNER JOIN paintingtitles ON (paintings.id = paintingtitles.painting_id) INNER JOIN languages paintingdescription_languages ON (paintingdescription_languages.id = paintingdescriptions.language_id) INNER JOIN languages paintingtitle_languages ON (paintingtitle_languages.id = paintingtitles.language_id) ", :conditions => cond, :page => page, :per_page => APP_CONFIG['per_page'], :order => "id DESC" ) Now I wonder if this is a correct way of doing this. I need to fetch paintings with their title and description in different functions, but I don't want to specify this long join statement each time. Is there a cleaner way, for instance making use of the has_many through? e.g. has_many :paintingdescription_languages, :through => :paintingdescriptions, :source => :language has_many :paintingtitle_languages, :through => :paintingtitles, :source => :language But if I implement above 2 lines together with the following ones, then only paintingtitles are filtered by language, and not the paintingdescriptions: cond = {"languages.code" => language_code} cond['paintings.publish'] = 1 unless admin paginate( :all, :select => ["paintings.id, paintings.publish, paintings.photo_file_name, paintingtitles.title, paintingdescriptions.description"], :joins => [:paintingdescription_languages, :paintingtitle_languages], :conditions => cond, :page => page, :per_page => APP_CONFIG['per_page'], :order => "id DESC" )

    Read the article

1