铁轨上的一对多关系红宝石

时间:2014-01-10 01:49:02

标签: mysql ruby-on-rails mysql2

所以我对subject.rb文件的更改进行了更改

class CreateSubjects < ActiveRecord::Migration
  def up 
    create_table :subjects do |t|

        t.string "name"
        t.integer "position"
        t.boolean "visible", :default =>false
     t.timestamps
    end


  end

  def down
    drop_table :subjects

  end

end

然后我继续并在

中定义了page.rb文件
class Page < ActiveRecord::Base
    belongs_to :subject
end

然后我到了终端

Muhammeds-MacBook-Pro:simple_cms muhammedz786 $ rails c 加载开发环境(Rails 4.0.2) IRB(主):001:0&GT; subject = Subject.find(1)   主题加载(0.3ms)SELECT subjects。* FROM subjects WHERE subjectsid = 1 LIMIT 1 =&GT; #

然后我试着调用subject.page方法,我得到了以下错误!这是什么意思?

 irb(main):002:0> subject.page
    **Mysql2::Error: Table 'simple_cms_development.pages' doesn't exist: SHOW FULL FIELDS FROM `pages`**
    ActiveRecord::StatementInvalid: Mysql2::Error: Table 'simple_cms_development.pages' doesn't exist: SHOW FULL FIELDS FROM `pages`
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `query'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `block in execute'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `execute'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/mysql2_adapter.rb:222:in `execute'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:301:in `execute_and_free'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:462:in `columns'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:118:in `block in prepare_default_proc'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:67:in `yield'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:67:in `columns_hash'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:25:in `column_for'
    ... 3 levels...
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:44:in `each_with_index'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:44:in `add_constraints'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:19:in `scope'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:103:in `association_scope'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:87:in `scope'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/singular_association.rb:42:in `find_target'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:147:in `load_target'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:54:in `reload'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/singular_association.rb:9:in `reader'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/builder/association.rb:70:in `page'
        from (irb):2
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
        from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
        from bin/rails:4:in `require'

1 个答案:

答案 0 :(得分:0)

确保您的Pages模型在迁移中有subject_id,然后确保运行rake db:migrate