1

I've got a problem that is giving me a headache.

in my Terminal I see that the Trail table is looked up. There is some dummy stuff in that table.

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

I've tried and erred on this for way to long, now this is using the same set up as Padrino's guide suggests in their "accepts_nested_attributes_for" guide. Still I can't get the form to show up in my browser for editing.

.../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"

In the html source there is rendered the group_trail but none of the forms with in.

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

in my models I have:

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

I did have a adventure_id in my trails table, but I've reverted to simpler method of trying for now, since it's said not to be needed for this association of nested_attributes.

If I run it with the adventure_id in trails table there is like it tries to select the adventure_id from the adventure table.

     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: 

Does anyone know why it searches the adventures table instead of trials table for the adventure_id?

Can anyone point at what I'm doing wrong? or point me in the right direction to figure this out?

Halakarta
  • 11
  • 3

1 Answers1

1

Maybe the problem is with the self reference.

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

What is the:

foreign_key ?

I.e:

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

Or if you have it in another table you should:

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

I think you can got the error also from your console:

Adventure.first.trails
DAddYE
  • 1,719
  • 11
  • 16
  • Thanks for pointing out the foreign key... It was something I forgot to define. Still it is not showing up in my rendered _form. But I've gone another way around it, that seems actually to be better in the end. You truly are the padrino of Padrino. – Halakarta Oct 26 '11 at 15:45
  • Well.. It Works! With in my view/_form.haml Thanks again. I wish I could backtrace all my steps in detail for the record but, I can't really remember all I did that directly related to this problem. – Halakarta Oct 26 '11 at 16:03