在两个不同关联上构建对象时视图中的nil对象

时间:2010-02-08 07:35:51

标签: ruby-on-rails

我对Ruby on Rails比较新,所以请不要介意我的新手级别!

我有以下模特:

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

在我的绘画新/编辑视图中,我想在每种语言中显示绘画细节及其标题和描述,以便我可以存储这些字段的翻译。

为了为我的绘画和每种语言构建语言标题和语言描述记录,我在我的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

在我在新/编辑视图中调用的form partial中,我有

<% 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 %>

但是,在运行代码时,ruby在评估paintingdescription.new_record时会遇到一个nil对象?:

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?

但是,如果我改变了我的顺序 a)在paintings_controller的新方法和方法中构建绘画标题和绘画描述 b)以部分形式显示绘画标题和绘画描述 然后我在paintingtitles.new_record上得到了零?调用

我总是把我建在第二位的物体弄得零。在我看来,我首先建立的并不是零。 我是否有可能无法同时为2个不同的关联构建对象?或者我错过了其他什么?

提前致谢!

1 个答案:

答案 0 :(得分:1)

实际上我找到了一个非常简单的解决方案。我在构建记录时提供了一个带有语言ID值的哈希值。

@languages = Language.all

@languages.each do |language|
   @painting.paintingtitles.build( {:language_id => language.id} )
   @painting.paintingdescriptions.build( {:language_id => language.id} )
end