【问题标题】:rails2 to rails3 upgrade problemrails2 到 rails3 升级问题
【发布时间】:2010-11-26 09:32:42
【问题描述】:

我正在尝试将我的应用程序从 Rails 2.3.5 升级到 Rails 3.0.3。

型号:
项目.rb

has_many :project_roles, :dependent => :destroy  
alias :roles :project_roles  
after_create :create_cal_for_project, :make_project_owner

def create_calendar_for_project
  self.calendars.create(:name => "default")
end

def make_project_owner
  #Make owner of project when created
  self.roles.create(:user => User.curr_user, :name => 'O')
end

项目角色.rb

belongs_to :project  
belongs_to :user  

project_roles 表列是 user_id、project_id、name
我遇到以下错误很难,请提出建议。

谢谢。

→ rails c
> Loading development environment (Rails 3.0.3)  

ruby-1.9.2-p0> User.curr_user = User.first  

=> #<User id: 1, username: "tispratik", login_email: "tispratik@gmail.com", > is_email_verified: nil, crypted_password: "deed6fa27ffefa57e63592a9b59295abf2660447cf281f34857...", password_salt: "YsD2nLte3pZy1FZ78GD", persistence_token: "84e44908be77b2dc1e41f8dfeacf9ef20c30050ff0c53854e0d...", single_access_token: "cp1bqVSheDh7vl9J48V", perishable_token: "6pGPzdw48nJnJAHsIZQW", last_login_ip: nil, last_login_at: nil, created_at: "2010-11-23 06:00:09", updated_at: "2010-11-23 06:00:09">  

ruby-1.9.2-p0> p = Project.create(:name => "ad", :description => "add")  
NoMethodError: undefined method `name' for nil:NilClass  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:57:in `block in visit_Arel_Nodes_InsertStatement'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:56:in `map'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:56:in `visit_Arel_Nodes_InsertStatement'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `visit'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:5:in `accept'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:19:in `block in accept'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:110:in `with_connection'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:17:in `accept'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/tree_manager.rb:19:in `to_sql'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/select_manager.rb:191:in `insert'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/relation.rb:14:in `insert'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/persistence.rb:270:in `create'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/timestamp.rb:47:in `create'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/callbacks.rb:281:in `block in create'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:413:in `_run_create_callbacks'  
... 30 levels...
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods/dirty.rb:21:in `save'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block (2 levels) in save'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block in save'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:248:in `rollback_active_record_state!'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:236:in `save'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/base.rb:498:in `create'  
 from (irb):2  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'  
 from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'  
 from script/rails:6:in `require'  
 from script/rails:6:in `<main>'

问题出在 make_project_owner 回调中。 create_calendar_for_project 运行良好。

我将 make_project_owner 更改为以下仍然出现相同的问题。

def make_project_owner
  #Make owner of project when created
  #role.create(:user => User.curr_user, :name => 'O')
  ProjectRole.create(:user => User.curr_user, :project => self, :name => "O")
end  

我尝试降级 Rails:
rails 3.0.2 -> 有同样的问题
rails 3.0.1 -> 有不同的错误

undefined method `name' for #<Arel::Value:0x00000005795090>  
Application Trace | Framework Trace | Full Trace  

arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `block in create'  
arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `each'  
arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `detect'  
arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `create'  
arel (1.0.1) lib/arel/algebra/relations/writes.rb:24:in `call'  
arel (1.0.1) lib/arel/session.rb:17:in `create'  
arel (1.0.1) lib/arel/algebra/relations/relation.rb:159:in `insert'  
activerecord (3.0.1) lib/active_record/relation.rb:14:in `insert'  
activerecord (3.0.1) lib/active_record/persistence.rb:271:in `create'  
activerecord (3.0.1) lib/active_record/timestamp.rb:47:in `create'  
activerecord (3.0.1) lib/active_record/callbacks.rb:281:in `block in create'  
activesupport (3.0.1) lib/active_support/callbacks.rb:413:in `_run_create_callbacks'  
activerecord (3.0.1) lib/active_record/callbacks.rb:281:in `create'  
activerecord (3.0.1) lib/active_record/persistence.rb:247:in `create_or_update'  
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `block in create_or_update'  
activesupport (3.0.1) lib/active_support/callbacks.rb:423:in `_run_save_callbacks'  
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `create_or_update'  
activerecord (3.0.1) lib/active_record/persistence.rb:39:in `save'  
activerecord (3.0.1) lib/active_record/validations.rb:43:in `save'  
activerecord (3.0.1) lib/active_record/attribute_methods/dirty.rb:21:in `save'  
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block (2 levels) in save'  
activerecord (3.0.1) lib/active_record/transactions.rb:289:in `block in     with_transaction_returning_status'  
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'  
activerecord (3.0.1) lib/active_record/transactions.rb:204:in `transaction'  
activerecord (3.0.1) lib/active_record/transactions.rb:287:in `with_transaction_returning_status'  
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block in save'  
activerecord (3.0.1) lib/active_record/transactions.rb:248:in `rollback_active_record_state!'  
activerecord (3.0.1) lib/active_record/transactions.rb:236:in `save'  
activerecord (3.0.1) lib/active_record/base.rb:498:in `create'  
app/models/project.rb:70:in `make_project_owner'  
activesupport (3.0.1) lib/active_support/callbacks.rb:463:in `_run_create_callbacks'  
activerecord (3.0.1) lib/active_record/callbacks.rb:281:in `create'  
activerecord (3.0.1) lib/active_record/persistence.rb:247:in `create_or_update'  
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `block in create_or_update'  
activesupport (3.0.1) lib/active_support/callbacks.rb:428:in `_run_save_callbacks'  
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `create_or_update'  
activerecord (3.0.1) lib/active_record/persistence.rb:39:in `save'  
activerecord (3.0.1) lib/active_record/validations.rb:43:in `save'  
activerecord (3.0.1) lib/active_record/attribute_methods/dirty.rb:21:in `save'  
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block (2 levels) in save'  
activerecord (3.0.1) lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'  
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'  
activerecord (3.0.1) lib/active_record/transactions.rb:204:in `transaction'  
activerecord (3.0.1) lib/active_record/transactions.rb:287:in `with_transaction_returning_status'  
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block in save'  
activerecord (3.0.1) lib/active_record/transactions.rb:248:in `rollback_active_record_state!'  
activerecord (3.0.1) lib/active_record/transactions.rb:236:in `save'  
app/controllers/projects_controller.rb:34:in `create'  
actionpack (3.0.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'  
actionpack (3.0.1) lib/abstract_controller/base.rb:150:in `process_action'  
actionpack (3.0.1) lib/action_controller/metal/rendering.rb:11:in `process_action'  
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'  
activesupport (3.0.1) lib/active_support/callbacks.rb:460:in `_run__2176748434120211400__process_action__1031157339911956458__callbacks'  
activesupport (3.0.1) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'  
activesupport (3.0.1) lib/active_support/callbacks.rb:93:in `run_callbacks'  
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action'  
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'  
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `block in instrument'  
activesupport (3.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'  
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `instrument'  
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'  
actionpack (3.0.1) lib/action_controller/metal/rescue.rb:17:in `process_action'  
actionpack (3.0.1) lib/abstract_controller/base.rb:119:in `process'  
actionpack (3.0.1) lib/abstract_controller/rendering.rb:40:in `process'  
actionpack (3.0.1) lib/action_controller/metal.rb:133:in `dispatch'  
actionpack (3.0.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'  
actionpack (3.0.1) lib/action_controller/metal.rb:173:in `block in action'  
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `call'  
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'  
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:27:in `call'  
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call'  
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize'  
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:75:in `optimized_each'  
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize'  
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call'  
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:492:in `call'  
haml (3.0.24) lib/sass/plugin/rack.rb:41:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/head.rb:14:in `call'  
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/flash.rb:182:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/cookies.rb:287:in `call'  
activerecord (3.0.1) lib/active_record/query_cache.rb:32:in `block in call'  
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'  
activerecord (3.0.1) lib/active_record/query_cache.rb:12:in `cache'  
activerecord (3.0.1) lib/active_record/query_cache.rb:31:in `call'  
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'  
activesupport (3.0.1) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'  
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:44:in `call'  
rack (1.2.1) lib/rack/sendfile.rb:107:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'  
railties (3.0.1) lib/rails/rack/logger.rb:13:in `call'  
rack (1.2.1) lib/rack/runtime.rb:17:in `call'  
activesupport (3.0.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'  
rack (1.2.1) lib/rack/lock.rb:11:in `block in call'  
<internal:prelude>:10:in `synchronize'  
rack (1.2.1) lib/rack/lock.rb:11:in `call'  
actionpack (3.0.1) lib/action_dispatch/middleware/static.rb:30:in `call'  
railties (3.0.1) lib/rails/application.rb:168:in `call'  
railties (3.0.1) lib/rails/application.rb:77:in `method_missing'  
railties (3.0.1) lib/rails/rack/log_tailer.rb:14:in `call'  
rack (1.2.1) lib/rack/content_length.rb:13:in `call'  
rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service'  
/home/pratik/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'  
/home/pratik/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'  
/home/pratik/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'  

【问题讨论】:

  • 请尝试更好地格式化。
  • 我无法阅读此乱码错误消息,但很可能是您的 :after_create 回调之一导致错误。尝试禁用它们,然后依次禁用它们,这应该会让你更接近找到原因(我认为不匹配的回调名称只是一个编辑错误)。
  • 更新了格式,使其看起来更好一些。仍然看起来很漂亮:(另外,我发现问题出在 make_project_owner 回调中,也尝试了上述更改,仍然是同样的错误。
  • 需要注意的是,Project 和 ProjectRole 模型都有 :name 字段,不确定是否存在名称冲突。
  • 看起来这是 Arel 的现有问题。如果有人调查它,我将不胜感激。谢谢!灯塔票rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/…

标签: ruby-on-rails activerecord arel


【解决方案1】:

我遇到了类似的问题,但我可以用以下代码解决这个问题:

module Connection
class DB1 < ActiveRecord::Base
    self.abstract_class = true

    establish_connection(Rails.env)
end

class DB2 < ActiveRecord::Base
    self.abstract_class = true

    establish_connection("#{Rails.env}_db2")
end
end

class Project < Connection::DB1

也许它对我有用,因为在所有模型中都使用了建立连接。

【讨论】:

    【解决方案2】:

    我没有时间对此进行测试,所以我不能肯定地说,但这条线在我看来很可疑:

    self.calendars.create(:name => "default")
    

    我觉得应该是这样的:

    Calendar.create(:name => "default", :project_id => self.id)
    

    【讨论】:

    • 问题出在 make_project_owner 回调中。
    • 我猜这是因为您正在调用 ProjectRole.create。尝试调用 Role.create。
    • 由于 Role 只是 ProjectRole 的别名,它不允许我使用它。我收到以下错误。未初始化的常量 Project::Role
    • 我终于可以使用硬编码的 sql 来运行它了。 def make_project_owner #创建项目时设为所有者 #self.roles.build(:user => User.curr_user, :name => 'O') connection.execute("插入到project_roles(project_id, user_id, name) values(# {self.id}, #{User.curr_user.id}, 'O')") 结束
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多