2012-05-12 7 views
0

私のビューであるstories/_form.slimに次の例外が発生します。それは、私がアクセスしようとしていないよ属性文句ているため、例外が奇妙に思える:現在アクセスされていないモデル属性に関するビューからSQL例外がスローされます

... 

= form_tag("/stories/#{@story.id}", :method => "put") do 
    = label_tag "Type the next line in the story. You must use the word '#{@story.curr_sentence.constraint.phrase}'." 

... 

モデル/物語:

SQLite3::SQLException: no such column: constraints.sentence_id: SELECT "constraints".* FROM "constraints" WHERE "constraints"."sentence_id" = 1 LIMIT 1 

問題のある行は話/ _form.slimで2番目の行です。 RB:

class Story < ActiveRecord::Base 
    has_many :sentences, :dependent => :destroy 
    accepts_nested_attributes_for :sentences, :allow_destroy => true 

    def curr_sentence 
    self.sentences.find_by_turn(self.turn) 
    end 

    ... 
end 

モデル/ sentence.rb:

class Sentence < ActiveRecord::Base 
    belongs_to :story 
    has_one :constraint 
    accepts_nested_attributes_for :constraint 
end 

モデル/ constraint.rb:

class Constraint < ActiveRecord::Base 
    has_many :sentences 
end 

デシベル/ schema.rb:

create_table "stories", :force => true do |t| 
    t.integer "turn",  :default => 1 
    t.datetime "created_at",    :null => false 
    t.datetime "updated_at",    :null => false 
    end 

    create_table "sentences", :force => true do |t| 
    t.integer "constraint_id" 
    t.integer "story_id" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    end 

    create_table "constraints", :force => true do |t| 
    t.string "phrase" 
    t.integer "constraint_category_id", :limit => 255 
    t.datetime "created_at",       :null => false 
    t.datetime "updated_at",       :null => false 
    end  

任意のアイデア?それを把握しようと私の耳を裂いている:)

答えて

0

文と制約の間の関連付けが間違っています。 has_oneは1対1の関係でのみ使用してください。これは1対多の関係ですので、belongs_toを使用してください。

class Sentence < ActiveRecord::Base 
    belongs_to :story 
    belongs_to :constraint 
    accepts_nested_attributes_for :constraint 
end 
+0

ありがとうございます!素晴らしい仕事をした。 –

関連する問題