根据https://stackoverflow.com/a/7795313/993890,引用应该与belongs_to的工作方式相同,但这是我所看到的。
$ 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
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
class CreateRecords < ActiveRecord::Migration
def change
create_table :records do |t|
t.string :title
t.timestamps
end
end
end
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
class Image < ActiveRecord::Base
belongs_to :record
end
(手动添加has_many:图像)
class Record < ActiveRecord::Base
has_many :images
end
一切看起来都不错。但是,实际上,当我尝试为Record创建一个Image时,Rails会创建一个新的空Record。以下是rails控制台。我在Windows上尝试过ruby 2.0.0,在Ubuntu上尝试过2.1.2。我觉得我错过了一些概念。有人可以解释为什么我会看到这种行为吗?谢谢!
无图像记录
> 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 []>
为记录创建图像实际上会创建一个新的空记录
> 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
图像对象属于记录2但记录1有
> 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 []>
每次保存图片时,都会创建一个新记录
> 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
答案 0 :(得分:0)
不要在Rails中使用Record作为模型名称。
我重命名为Record to Work,该应用程序按预期工作。