Ruby on rails 为什么t.references关系不能创建正确的模型对象
根据参考资料,它的工作方式应该和你所属的一样,但以下是我所看到的 设置 schema.rb 迁移/创建_records.rb 迁移/创建_images.rb app/models/image.rb app/models/record.rb 手动添加多个:图像Ruby on rails 为什么t.references关系不能创建正确的模型对象,ruby-on-rails,Ruby On Rails,根据参考资料,它的工作方式应该和你所属的一样,但以下是我所看到的 设置 schema.rb 迁移/创建_records.rb 迁移/创建_images.rb app/models/image.rb app/models/record.rb 手动添加多个:图像 class Record < ActiveRecord::Base has_many :images end 问题 一切看起来都很好。然而,在实践中,当我尝试为记录创建图像时,Rails会创建一个新的空记录。以下是来自rails
class Record < ActiveRecord::Base
has_many :images
end
问题
一切看起来都很好。然而,在实践中,当我尝试为记录创建图像时,Rails会创建一个新的空记录。以下是来自rails控制台的内容。我在Windows上试过ruby 2.0.0,在Ubuntu上试过ruby 2.1.2。我觉得我缺少了一些概念。有人能解释为什么我会看到这种行为吗?谢谢
没有图像的记录
为记录创建图像实际上创建了一个新的空记录
图像对象属于记录2,但记录1拥有它
每次我保存图像时,它都会创建一条新记录
不要在Rails中将记录用作模型名
我将Record重命名为Work,应用程序按预期工作。读到你的问题,我感到震惊。我遵循您所做的所有步骤,但在我的机器上,它与rails 4.1.4完美配合。我们从另一个组继承了此项目,并花了一段时间将记录重命名为工作状态,然后进行测试,但应用程序/模型现在按预期工作。感谢您的回答
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
class Record < ActiveRecord::Base
has_many :images
end
> 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
> 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