0

我的项目中有两个模型类。第一个是User

class User < ActiveRecord::Base
  attr_accessible :email, :name
  has_many :micropost1s
end

另一个是Micropost1

class Micropost1 < ActiveRecord::Base
  attr_accessible :content, :user_id
  belongs_to :user

  validates :content, :length => { :maximum => 140 }
end

我没有得到我错的地方。首先我运行这个命令来分配变量first_user

first_user = User.first

之后我在控制台中写:

first_user.micropost1s

我收到此错误日志:

 Micropost1 Load (0.3ms)  SELECT "micropost1s".* FROM "micropost1s" WHERE "micropost1s"."user_id" = 1
SQLite3::SQLException: no such column: micropost1s.user_id: SELECT "micropost1s".* FROM "micropost1s"  WHERE "micropost1s"."user_id" = 1
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: micropost1s.user_id: SELECT "micropost1s".* FROM "micropost1s"  WHERE "micropost1s"."user_id" = 1
    from /var/lib/gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
    from /var/lib/gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
    from /var/lib/gems/1.8/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/sqlite_adapter.rb:246:in `exec_query'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `log'
    from /var/lib/gems/1.8/gems/activesupport-3.2.9/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/sqlite_adapter.rb:467:in `select'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/querying.rb:38:in `find_by_sql'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/explain.rb:40:in `logging_query_plan'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/querying.rb:37:in `find_by_sql'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/relation.rb:171:in `exec_queries'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/relation.rb:160:in `to_a'
... 5 levels...
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `__send__'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
    from /var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
    from /usr/lib/ruby/1.8/irb.rb:310:in `output_value'
    from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input'
    from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status'
    from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input'
    from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input'
    from /usr/lib/ruby/1.8/irb.rb:71:in `start'
    from /usr/lib/ruby/1.8/irb.rb:70:in `catch'
    from /usr/lib/ruby/1.8/irb.rb:70:in `start'
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
    from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:41
    from script/rails:6:in `require'
    from script/rails:6irb(main):008:0> 
4

1 回答 1

2

听起来您还没有运行通过创建模型生成的迁移:

$ rake db:migrate

如果这不起作用,您可能需要在user_idMicropost1 模型中添加一列。例如:

$ rails g migration add_user_id_to_micropost1s

然后编辑生成的迁移以根据需要创建字段。或者,您可以使用对用户的引用重新创建模型:

$ rails g model Micropost1 something:string user:references
于 2012-12-17T14:13:45.557 回答