0

Class 是一个保留字,所以我将表名从class_assignmentto更改cassignments为避免任何可能导致它的错误(尽管我有class_instruction(s)。我尝试了复数和单数版本,但仍然得到错误。cassignments表是中间assignments和之间的多对多关系表class_instructions

架构:

ActiveRecord::Schema.define(:version => 20130524160107) do

  create_table "assignments", :force => true do |t|
    t.string   "name"
    t.string   "fractionRepresentations"
    ....etc...
    t.datetime "created_at",                  :null => false
    t.datetime "updated_at",                  :null => false
  end

  create_table "cassiggnments", :force => true do |t|
    t.integer  "class_instruction_id"
    t.integer  "assignment_id"
    t.datetime "created_at",           :null => false
    t.datetime "updated_at",           :null => false
  end

  add_index "cassiggnments", ["assignment_id"], :name => "index_class_assignment_on_assignment_id"
  add_index "cassiggnments", ["class_instruction_id"], :name => "index_class_assignment_on_class_instruction_id"

  create_table "class_instructions", :force => true do |t|
    t.string   "name"
    t.datetime "time"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "person_id"
  end

  add_index "class_instructions", ["person_id"], :name => "index_class_instructions_on_person_id"

  ....etc....

end

尝试在 seed.rb 中播种数据库

# ClassInstruction Assignment
  cia1 = Cassignment.create({ class_instruction_id: 4, assignment_id: 1 }, :without_protection => true ).save!

我得到了错误:

rake aborted!
Could not find table 'cassignments'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb:465:in `table_structure'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/connection_adapters/sqlite_adapter.rb:346:in `columns'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/connection_adapters/schema_cache.rb:12:in `block in initialize'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `yield'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `default'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:228:in `columns'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:243:in `column_defaults'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/base.rb:479:in `initialize'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/persistence.rb:44:in `new'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/persistence.rb:44:in `create'
/Users/special/dev/SoundOfFractions/db/seeds.rb:83:in `<top (required)>'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:245:in `load'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:245:in `block in load'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:236:in `load_dependency'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:245:in `load'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/railties-3.2.2/lib/rails/engine.rb:520:in `load_seed'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/railties/databases.rake:309:in `block (2 levels) in <top (required)>'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:205:in `block in invoke_prerequisites'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:203:in `each'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:203:in `invoke_prerequisites'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:183:in `block in invoke_with_call_chain'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.2/lib/active_record/railties/databases.rake:252:in `block (2 levels) in <top (required)>'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/Users/special/.rvm/gems/ruby-1.9.3-p362/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/Users/special/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in `eval'
/Users/special/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:setup => db:seed

唯一奇怪的是,尽管我进行了迁移,但我没有得到cassignment.rb模型,所以我不得不手动创建它(也许我没有使用脚手架,这是我通常习惯做的,但我离题了)。所以为了后代,这里是cassignment.rb

class Cassignment < ActiveRecord::Base
  belongs_to :class_instruction
  belongs_to :assignment
end

命名复数匹配其他多对多关系表和种子结构,但我不知道我哪里出错了,除了理解种子文件找不到表,根据sqlite浏览器确实存在,并且正确地称为cassignments(复数)。

4

2 回答 2

1

您已经创建了一个名为 cassiggnments 的表,其中包含两个 'g'

于 2013-05-24T18:35:12.557 回答
0

这是has_and_belongs_to_many关系还是has_many :through关系?

仅 HABTM: 在前一种情况下,您不需要模型,并且表名必须是按字母顺序排列的两个连接表名,这一点很重要。assignments_class_instructions

在这两种情况下,创建连接的正确方法是将一个类的实例添加到另一个类的实例,而不是直接创建连接。

class_instruction = ClassInstruction.find(4)
class_instruction.assignments << Assignment.find(1)

此外,class_assignmentsandClassAssignment作为表名和模型名没有问题,所以这不是问题。

于 2013-05-24T16:27:27.317 回答