0

在我看来,我遇到了以下异常,storys/_form.slim。该异常似乎很奇怪,因为它抱怨我不想访问的属性:

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

违规行是 stories/_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

模型/句子.rb:

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

模型/约束.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    

有任何想法吗?一直在撕扯我的耳朵,试图弄清楚:)

4

1 回答 1

0

句子和约束之间的关联是错误的。您应该只has_one在 1:1 的关系中使用。这是一个一对多的关系,所以你应该使用belongs_to

class Sentence < ActiveRecord::Base
  belongs_to :story
  belongs_to :constraint
  accepts_nested_attributes_for :constraint
end
于 2012-05-12T03:32:56.427 回答