gpt4 book ai didi

ruby-on-rails - 一对一关系错误:ActiveModel::MissingAttributeError:无法写入未知属性

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:34 25 4
gpt4 key购买 nike

我正在开发一个测试应用程序来学习 Rails。在玩关联时,我收到 ActiveModel::MissingAttributeError: can't write unknown attribute 'linked_section_id' 错误。

让我先解释一下要求。用户选择一本书的一部分进行查看。在每个部分的末尾,会有一些与其他部分相关联的问题。如果用户点击任何问题链接,他将被重定向到相关部分。

因此实体关系如下所述:

  • 一个部分可以有多个问题(一对多)
  • 一个问题只属于一个部分(多对一 - 一对多的反面)
  • 一个问题可以链接到不同的部分(一对一)

环境: ruby 1.9.3, rails 3.2.2拥有所有必需的 gem

这里是模型类:

class Section < ActiveRecord::Base

# Associations
has_many :questions

end

class Question < ActiveRecord::Base
belongs_to :section, :foreign_key => :section_id
has_one :linked_section, :class_name => "Section", :foreign_key => "linked_section_id"
end

db/schema.rb
.
.
create_table "sections", :force => true do |t|
t.string "name"
t.string "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "questions", :force => true do |t|
t.string "description"
t.integer "section_id"
t.integer "linked_section_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
.
.

这是规范。

question_spec.rb
require 'spec_helper'

describe Question do
before do
@question = Question.new(description:'Test')
end

subject { @question }

# Check for attribute accessor methods
it { should respond_to(:description) }
it { should respond_to(:section) }
it { should respond_to(:linked_section) }
it { should respond_to(:linked_section_id) }
end

section_spec.rb
require 'spec_helper'

describe Section do
before do
@section = Section.new(name:'Test')
end

subject { @section }

# Check for attribute accessor methods
it { should respond_to(:name) }
it { should respond_to(:questions) }

# Sanity check, verifying that the @section object is initially valid
it { should be_valid }

describe "Question should have one action item while persisted" do
pending "This is broken after changing entity relationship"
before {
@section = Section.new(name: 'Section 2')

linked_section = Section.new(name: 'Action Section')
linked_section.save

question = Question.new(description: 'What next?')
question.linked_section = linked_section
@section.questions << question
@section.save
.
.
.

}
.
.
.
end

end

现在 question_spec.rb 没有任何错误地通过了,但是 ActiveModel::MissingAttributeError: can't write unknown attribute 'linked_section_id' error on line question.linked_section = linked_section .

但是如果你注意到,下面是 question_spec.rb 的例子

it { should respond_to(:linked_section) }
it { should respond_to(:linked_section_id) }

顺利通过。这意味着问题有“linked_section_id”。那么为什么 rspec 会出现 panic ?

我怀疑为将问题链接到不同部分而定义的关系。谁能指导我正确设计模型?

谢谢,阿米特·帕特尔

最佳答案

您的规范正在检查问题是否响应 :linked_section_id (读者方法),而不是它是否响应 :linked_section_id= (作者方法)。表中的所有字段都将具有与之关联的读取器方法,并且由于您在表中添加了 linked_section_id 字段,因此读取器方法就在那里。

无论何时向表添加外键,它都应该是关联的 *belongs_to* 端。在这里,您使用了 has_one。将其切换为 belongs_to,并在 Section 模型中,放置

has_one :linked_section, :class_name => 'Question'

希望这对您有所帮助,

关于ruby-on-rails - 一对一关系错误:ActiveModel::MissingAttributeError:无法写入未知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9743252/

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