2012-03-16 2 views
2

Railsを学ぶためのテストアプリケーションを開発しています。アソシエーションで遊んでいるうちにActiveModel::MissingAttributeError: can't write unknown attribute 'linked_section_id'エラーが発生しています。1対1関連のエラー:ActiveModel :: MissingAttributeError:不明な属性を書き込めません

最初に要件を説明しましょう。ユーザーは、ブックのセクションを表示するために選択します。各セクションの終わりには、いくつかの別のセクションにリンクされているいくつかの質問があります。ユーザーが質問リンクをクリックすると、関連するセクションにリダイレクトされます。

下記のように実体の関係は次のとおりです。

  • セクションは、質問だけ(多くの1に単一のセクションに属している複数の質問(多くの1つ)
  • を持つことができます - 1のフリップサイド多くの)
  • 質問が異なる部分(一方のいずれかに連結することができる)

環境: ルビー1.9.3、 レール3.2.2 0必要なすべての宝石とここ

はモデルクラスである:ここでは

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は、エラーが、ラインquestion.linked_section = linked_sectionActiveModel::MissingAttributeError: can't write unknown attribute 'linked_section_id'エラーを取得せずに渡します。

ていますが、エラーなしで、question_spec.rbの以下の例は、

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

パスを気づけば。これはQuestionが 'linked_section_id'を持っていることを意味します。だからrspecのパニックはなぜですか?

質問を別のセクションにリンクするために定義された関係には疑問があります。誰もがモデルを正しく設計するために私を導くことができますか?

おかげで、アミットパテル

答えて

1

はあなたのスペックは疑問が応答するか否かを確認されていますlinked_section_id(リーダーメソッド)、それが応答するかどうかかどうか:linked_section_id =(ライター・メソッド)。テーブルのすべてのフィールドには、それらに関連付けられたリーダーメソッドがあり、テーブルにlinked_section_idフィールドを追加したので、リーダーメソッドがそこにあります。

テーブルに外部キーを追加するときは、その外部キーを関連の* belongs_to *側にする必要があります。ここでは、has_oneを使用しました。 belongs_toのにそれを切り替えて、セクションモデルで、

has_one :linked_section, :class_name => 'Question' 

うまくいけば、このことができ、

+1

おかげでルークを入れて!出来た。私は実際には、linked_section_idがforeign_keyであっても、belongs_toでなければならないhas_oneという関係を指定しています。また、私はgetterメソッドがsetterではないことを確認しています。今私は1つの質問があります。セクションから質問に移動したくない場合でも、セクションモデルで 'has_one:linked_section、:class_name => 'Question''を指定する必要がありますか?基本的に私はJava開発者であり、JPAでは1対1の単方向関係を定義できます。 –

関連する問題