0

According to https://stackoverflow.com/a/7795313/993890 references should work the same way as belongs_to but here's what I'm seeing.

setup

$ rails new refs
$ rake db:migrate
$ rake db:setup
$ rails g model Record title:string
$ rake db:migrate
$ rails g model Image image_url:text record:references
$ rake db:migrate

schema.rb

create_table "images", force: true do |t|
  t.text     "image_url"
  t.integer  "record_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "images", ["record_id"], name: "index_images_on_record_id", using: :btree

create_table "records", force: true do |t|
  t.string   "title"
  t.datetime "created_at"
  t.datetime "updated_at"
end

migrate/create_records.rb

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

      t.timestamps
    end
  end
end

migrate/create_images.rb

class CreateImages < ActiveRecord::Migration
  def change
    create_table :images do |t|
      t.text :image_url
      t.references :record, index: true

      t.timestamps
    end
  end
end

app/models/image.rb

class Image < ActiveRecord::Base
  belongs_to :record
end

app/models/record.rb

(manually add has_many :images)

class Record < ActiveRecord::Base
  has_many :images
end

question

Everything looks good. However, in practice, when I try to create an Image for a Record, Rails creates a new, empty Record instead. The following is from rails console. I've tried ruby 2.0.0 on Windows and 2.1.2 on Ubuntu. I feel like I'm missing some concept. Can someone explain why I'm seeing this behavior? Thanks!

Record with no Images

> r = Record.create( title: 'foo' )
   (0.0ms)  BEGIN
  SQL (0.0ms)  INSERT INTO "records" ("created_at", "title", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  
[["created_at", "2014-10-30 14:44:01.126080"], ["title", "foo"], ["updated_at", "2014-10-30 14:44:01.126080"]]
   (46.9ms)  COMMIT
=> #<Record id: 1, title: "foo", created_at: "2014-10-30 14:44:01", updated_at: "2014-10-30 14:44:01">

> Record.count
   (0.0ms)  SELECT COUNT(*) FROM "records"
=> 1

> r.images
  Image Load (0.0ms)  SELECT "images".* FROM "images"  WHERE "images"."record_id" = $1 
[["record_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy []>

create an Image for the Record actually creates a new, empty Record

> i = r.images.create( image_url: 'http://image.com' )    (0.0ms)  BEGIN   SQL (0.0ms)  INSERT INTO "records" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"
[["created_at", "2014-10-30 14:50:37.571941"], ["updated_at", "2014-10-30 14:50:37.571941"]]    (46.9ms)  COMMIT
=> #<Image id: nil, image_url: "http://image.com", record_id: 2, created_at: nil, update d_at: nil>

> Record.count    (0.0ms)  SELECT COUNT(*) FROM "records"
=> 2

> Image.count    (0.0ms)  SELECT COUNT(*) FROM "images"
=> 0

Image object belongs to Record 2 but Record 1 has it

> r
=> #<Record id: 1, title: "bar", created_at: "2014-10-30 16:15:18", updated_at: "2014-10-30 16:15:18">
> r.id
=> 1
> r.images
=> #<ActiveRecord::Associations::CollectionProxy [#<Image id: nil, image_url: "http://image.com", record_id: 2, created_at: nil, updated_at: nil>]>
> i
=> #<Image id: nil, image_url: "http://image.com", record_id: 2, created_at: nil, updated_at: nil>
> i.record_id
=> 2
> Record.find(2).images
  Record Load (0.0ms)  SELECT  "records".* FROM "records"  WHERE "records"."id" = $1 LIMIT 1  
[["id", 2]]
  Image Load (0.0ms)  SELECT "images".* FROM "images"  WHERE "images"."record_id" = $1
[["record_id", 2]]
=> #<ActiveRecord::Associations::CollectionProxy []>

every time I save the Image, it creates a new Record

> Record.last
  Record Load (0.0ms)  SELECT  "records".* FROM "records"   ORDER BY "records"."id" DESC LIMIT 1
=> #<Record id: 2, title: nil, created_at: "2014-10-30 16:15:58", updated_at: "2014-10-30 16:15:58">
> i.save
   (0.0ms)  BEGIN
  SQL (0.0ms)  INSERT INTO "records" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  
[["created_at", "2014-10-30 16:18:21.279150"], ["updated_at", "2014-10-30 16:18:21.279150"]]
   (62.5ms)  COMMIT
=> true
> Record.count
   (0.0ms)  SELECT COUNT(*) FROM "records"
=> 3
> Image.count
   (0.0ms)  SELECT COUNT(*) FROM "images"
=> 0
Community
  • 1
  • 1
ryanttb
  • 1,278
  • 13
  • 20
  • I was shocked to read your question. And I follow all the steps you do, but it works perfect with rails 4.1.4, on my machine. – Alejandro Babio Oct 31 '14 at 17:59
  • Turns out Record is a "word that causes trouble" in Rails ( http://reservedwords.herokuapp.com/words/record?q[word_or_notes_cont]=Record ). We inherited this project from another group and it took a while to rename Record to Work and then test but the app/models work as expected now. – ryanttb Nov 03 '14 at 17:05

1 Answers1

0

Do not use Record as a model name in Rails.

I renamed Record to Work and the app works as expected.

ryanttb
  • 1,278
  • 13
  • 20