publisher = Publisher.create name: 'Gi-6ren">
gpt4 book ai didi

ruby-on-rails - "can' t 写入未知属性”

转载 作者:行者123 更新时间:2023-12-05 06:44:47 25 4
gpt4 key购买 nike

我按照我的教科书所说的那样运行,但是出现了错误。 这是一个图书管理应用程序。

我要运行什么

irb(main):001:0>publisher = Publisher.create name: 'Gihyo inc.', address: 'Ichigaya'
irb(main):002:0>publisher.books << Book.find(1)
irb(main):003:0>publisher.books.to_a

结果

首先,

irb(main):001:0>publisher = Publisher.create name: 'Gihyo inc.', address: 'Ichigaya'

似乎成功了。

接下来,

irb(main):002:0>publisher.books << Book.find(1)

失败。

结果如下。

 Book Load(0.6ms) SELECT "books".* FROM "books" WHERE "books"."id" = ? LIMIT 1 [["id", 1]]
(4.0ms) begin transaction
(0.3ms) rollback transaction
ActiveModel::MissingAttributeError: can't write unknown attribute `publisher_id'
from ...

相关模型

book.rb

class Book < ActiveRecord::Base
scope :costly, ->{where("price>?" ,3000) }

belongs_to :publisher
end

publisher.rb

class Publisher < ActiveRecord::Base
has_many :books
end

相关迁移

20141218113551_create_publishers.rb

class CreatePublishers < ActiveRecord ::Migration
def change
create_table :publishers do |t|
t.string :name
t.text :address

t.timestamps
end
end
end

20141218113811_add_publisher_id_to_books.rb

class AddPublisherIdToBooks < ActiveRecord::Migration
def change
add_reference :books, :publisher, index: true
end
end

怎么了?

最佳答案

我修改后修复了这个错误-

belongs_to :user, counter_cache: :true

对此 - belongs_to :user, counter_cache: true

请注意 bool 值,我将其用作符号...这是完全不正确的。

关于ruby-on-rails - "can' t 写入未知属性”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27548331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com