我在视图中遇到以下异常:stories / _form.slim。异常似乎很奇怪,因为它抱怨我不想访问的属性:
SQLite3::SQLException: no such column: constraints.sentence_id: SELECT "constraints".* FROM "constraints" WHERE "constraints"."sentence_id" = 1 LIMIT 1
违规行是故事中的第二行/ _form.slim:
...
= 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}'."
...
模型/ story.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 :(得分:0)
句子和约束之间的关联是错误的。您应该只以1:1的关系使用has_one
。这是一对多的关系,因此您应该使用belongs_to
:
class Sentence < ActiveRecord::Base
belongs_to :story
belongs_to :constraint
accepts_nested_attributes_for :constraint
end