1

我有一个让我头疼的问题。

在我的终端中,我看到 Trail 表已被查找。那张桌子上有一些虚拟的东西。

DEBUG - Account Load (0.4ms)  SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = 1 LIMIT 1
DEBUG - Trail Load (0.2ms)  SELECT "trails".* FROM "trails"
DEBUG - TEMPLATE (0.0004ms) /adventures/new
DEBUG - TEMPLATE (0.0002ms) /layouts/application
DEBUG - TEMPLATE (0.0004ms) /adventures/_form
DEBUG - TEMPLATE (0.0003ms) /base/_sidebar
DEBUG -      GET (0.0644ms) /admin/adventures/new - 200 OK

我已经尝试并犯了很长时间的错误,现在这是使用与帕德里诺指南在他们的“accepts_nested_attributes_for”指南中建议的相同的设置。我仍然无法让表单显示在我的浏览器中进行编辑。

.../views/adventures/_form.haml

.group_trail
  -f.fields_for :trails do |trail_form|
    =trail_form.label :start
    =trail_form.text_field :start
=trail_form.label :end
    =trail_form.text_field :end
    =trail_form.label :via
    =trail_form.text_field :via
    -unless trail_form.object.new_record?
      =trail_form.check_box '_destroy'
      =trail_form.label '_destroy', :caption => "Remove"

在 html 源代码中,呈现了 group_trail 但没有带有 in 的表单。

...     
</div>
 <div class='group_trail'>
  </div>
    <div class='group navform wat-cf'>
    <input class="button" value="Save" type="submit" />

在我的模型中,我有:

class Adventure < ActiveRecord::Base  
    has_many :trails, :class_name => 'Adventure'
    accepts_nested_attributes_for :trails, :allow_destroy => true
end

class Trail < ActiveRecord::Base 
     belongs_to :adventure
end

我的 trails 表中确实有一个 Adventure_id,但我现在已经恢复到更简单的尝试方法,因为据说这个 nested_attributes 关联不需要它。

如果我使用 trails 表中的 Adventure_id 运行它,就像它试图从 Adventure 表中选择 Adventure_id 一样。

     DEBUG - Account Load (0.3ms)  SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = 1 LIMIT 1
      DEBUG - Trail Load (0.2ms)  SELECT "trails".* FROM "trails"
      DEBUG - Adventure Load (0.2ms)  SELECT "adventures".* FROM "adventures" WHERE "adventures"."id" = ? LIMIT 1  [["id", "9"]]
      DEBUG - TEMPLATE (0.0004ms) /adventures/edit
      DEBUG - TEMPLATE (0.0002ms) /layouts/application
      DEBUG - TEMPLATE (0.0003ms) /adventures/_form
      DEBUG - Adventure Load (0.3ms)  SELECT "adventures".* FROM "adventures" WHERE "adventures"."adventure_id" = 9
      DEBUG - SQLite3::SQLException: no such column: adventures.adventure_id: SELECT "adventures".* FROM "adventures"  WHERE "adventures"."adventure_id" = 9
    ActiveRecord::StatementInvalid - SQLite3::SQLException: no such column: adventures.adventure_id: SELECT "adventures".* FROM "adventures"  WHERE "adventures"."adventure_id" = 9: 

有谁知道为什么它在冒险表而不是试验表中搜索 Adventure_id?

谁能指出我做错了什么?或者指出我正确的方向来解决这个问题?

4

1 回答 1

1

也许问题出在自我参考上。

class Adventure < ActiveRecord::Base  
  has_many :trails, :class_name => 'Adventure'
  accepts_nested_attributes_for :trails, :allow_destroy => true
end

是什么:

foreign_key ?

IE:

has_many :trails, :class_name => 'Adventure', :foreign_key => :trail_id

或者,如果您将它放在另一个表中,您应该:

has_many :trails, :class_name => 'Adventure', :foreign_key => :trail_id, :table_name => :trails

我认为您也可以从控制台收到错误:

Adventure.first.trails
于 2011-10-26T09:28:20.687 回答