0

The Issue

The rails console will not save my record to the database if it has a belongs_to association.

2.1.1 :002 > Track.create name: 'asdfasdf'
(0.1ms)  begin transaction
SQL (0.6ms)  INSERT INTO "records" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2014-06-12 15:58:20.868095"], ["updated_at", "2014-06-12 15:58:20.868095"]]
(45.9ms)  commit transaction
=> #<Track id: nil, record_id: 1, name: "asdfasdf", created_at: nil, updated_at: nil>



class Record < ActiveRecord::Base
  has_many :tracks
  accepts_nested_attributes_for :tracks, allow_destroy: true
end

class Track < ActiveRecord::Base
  belongs_to :record
end

class CreateRecords < ActiveRecord::Migration
  def change
    create_table :records do |t|
      t.string :title
      t.timestamps
    end
  end
end

class CreateTracks < ActiveRecord::Migration
  def change
    create_table :tracks do |t|
      t.belongs_to :record
      t.string :name
      t.timestamps
    end
  end
end

As you can see the :id field is nil. The :record_id field gets incremented instead. I have tried resetting the primary key and the issue still persists. This is however not my main beef because the record is not even saved after the insert.

2.1.1 :003 > Track.last
Track Load (0.3ms)  SELECT  "tracks".* FROM "tracks"   ORDER BY "tracks"."id" DESC LIMIT 1
=> nil

This is an issue because it prevents me from using nested forms like this one. Indecently when I download and run that rails app it works perfectly.

2.1.1 :002 > Answer.create content: 'swwaaagggggs'
(0.2ms)  begin transaction
SQL (0.8ms)  INSERT INTO "answers" ("content", "created_at", "updated_at") VALUES (?, ?, ?)  [["content", "swwaaagggggs"], ["created_at", "2014-06-12 16:46:04.989529"], ["updated_at", "2014-06-12 16:46:04.989529"]]
(58.9ms)  commit transaction
=> #<Answer id: 3, question_id: nil, content: "swwaaagggggs", created_at: "2014-06-12 16:46:04", updated_at: "2014-06-12 16:46:04">

Yet when I recreate it exactly myself I run into the same problem.

Strangely

If I rollback migrations and remove the association I get this.

2.1.1 :003 > Track.create name: 'swagger'
(0.2ms)  begin transaction
SQL (0.7ms)  INSERT INTO "records" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2014-06-12 16:06:12.443845"], ["updated_at", "2014-06-12 16:06:12.443845"]]
(0.2ms)  rollback transaction
ActiveModel::MissingAttributeError: can't write unknown attribute `record_id'



class Record < ActiveRecord::Base
end

class Track < ActiveRecord::Base
end

class CreateRecords < ActiveRecord::Migration
  def change
    create_table :records do |t|
      t.string :title
      t.timestamps
    end
  end
end

class CreateTracks < ActiveRecord::Migration
  def change
    create_table :tracks do |t|
      t.string :name
      t.timestamps
    end
  end
end

Note the reference to :record_id when the column no longer exists

I am at a loss.

Google has run out of pages and nobody else seem to have this issue which had led me to think that there has been something wrong with my code for the last 2 days; "something this well documented shouldn't be this hard," I thought. If anyone can help me figure this out I would be grateful.

Please let me know if you require more information this is the first time I have felt the need to post to StackOverflow.

colin
  • 52
  • 9
  • Did you try using simply t.integer :record_id instead of t.belongs_to :record in the migration ? – Rafal Jun 12 '14 at 17:02
  • Here is the output of this. StandardError: An error has occurred, this and all later migrations canceled: you can't redefine the primary key column 'id'. – colin Jun 12 '14 at 17:34

1 Answers1

3

Record is probably a reserved key word in rails ?

http://reservedwords.herokuapp.com/words/record?q[word_or_notes_cont]=record

mestachs
  • 1,889
  • 15
  • 17