【问题标题】:Rails habtm grief am I dumb or rails bug? (desperate ; )Rails habtm 悲伤我是哑巴还是 rails bug? (绝望的 ; )
【发布时间】:2011-04-12 18:03:11
【问题描述】:

大家好,

我在 habtm 关系方面遇到了一个奇怪的问题,老实说,我开始认为我可能在 rails 3 中偶然发现了一些奇怪的错误。不过,我肯定是疯了。我已经为此苦苦挣扎了 3 天,在阳光下搜索了所有我能想到的东西,但仍然无法找到答案。

好的,情况:

我正在创建一个 Rails 应用程序来替换 Java 应用程序和 PHP 应用程序(Java 应用程序和 php 前端)。这将是一个分阶段的操作,第一阶段是 Rails 应用程序接管注册和计费。为此,Rails 应用程序必须在数据库中为 Java 和 PHP 应用程序创建数据。 Rails 应用程序本身正在使用 Devise 进行身份验证。

在 database.yml 中,我定义了我的标准 3 个数据库,还为 Java 应用程序数据库定义了一个连接。

以下是外部对象的模型定义片段(我只是创建常规 Rails 模型以与外部数据库通信):

class Pushbroom::UserAccount < ActiveRecord::Base
  require 'digest/md5'
  require 'base64'

  establish_connection :pushbroom
  set_table_name :user_account
  set_primary_key :id

  has_and_belongs_to_many :user_roles, :join_table => 'pb_prod.users_roles', :class_name => 'Pushbroom::UserRole', :foreign_key => 'user_account_id', :association_foreign_key => 'user_role_id'
  belongs_to :user, :dependent => :destroy


  attr_accessible :user_roles, :admin_notes, :enabled, :username, :password_hash, :prefStore, :accepted_tos, :do_not_contact
end


class Pushbroom::UserRole < ActiveRecord::Base

  establish_connection :pushbroom
  set_table_name :user_role
  set_primary_key :id

  has_and_belongs_to_many :user_accounts, :join_table => 'pb_prod.users_roles', :class_name => 'Pushbroom::UserAccount', :foreign_key => 'user_role_id', :association_foreign_key => 'user_account_id'


end

最后是我的 Rails 应用程序用户对象:

class User < ActiveRecord::Base

  after_create :send_welcome_email
  before_save :create_pushbroom_user_data

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  belongs_to :pb_user_account, :class_name => "Pushbroom::UserAccount", :foreign_key => "pb_user_account_id", :dependent => :destroy, :autosave => true

  # Setup accessible (or protected) attributes for your model
  attr_accessible :first_name, :last_name, :username, :dob, :email, :password,         :password_confirmation, :remember_me

  validates_presence_of :first_name, :last_name, :username, :dob
  validates_date :dob, :on_or_after => lambda { 100.years.ago }, :on_or_after_message => "must be on or after #{100.years.ago.strftime('%m-%d-%Y')}" 
  validates_date :dob, :on_or_before => lambda { 13.years.ago }, :on_or_before_message => "must be on or before #{13.years.ago.strftime('%m-%d-%Y')}"

  def create_pushbroom_user_data
    pb_user = create_pushbroom_user
    pb_user_account = create_pushbroom_user_account(pb_user)
    pb_user_account.user_roles << Pushbroom::UserRole.find_by_name('user')
    self.pb_user_account = pb_user_account
  end

  def create_pushbroom_user
    pb_user = Pushbroom::User.new
    pb_user.attributes = self.attributes.slice(
      "email",
      "first_name",
      "last_name",
      "dob")

    pb_user
  end

def create_pushbroom_user_account(pb_user)
    pb_user_account = Pushbroom::UserAccount.new
    pb_user_account.enabled = true
    pb_user_account.password_hash =                 Pushbroom::UserAccount.create_password_digest(@plaintext_password, self.username)
    pb_user_account.username = self.username
    pb_user_account.user = pb_user

    pb_user_account
  end

看起来应该很香草。这里唯一的奇怪之处是它们不在本机 rails 数据库中,并且其中一个字段在关系表中被命名为 fun。

所以这是一个 rails 控制台会话,我在其中创建了一个 rails 用户,调用该方法来创建外部对象,然后尝试保存:

ruby-1.9.2-p180 :001 > def user_fred
ruby-1.9.2-p180 :002?>       { 
ruby-1.9.2-p180 :003 >             :first_name => "Fred",
ruby-1.9.2-p180 :004 >             :last_name => "Flinstone",
ruby-1.9.2-p180 :005 >             :username => "fflint",
ruby-1.9.2-p180 :006 >             :dob => "1986-06-01",
ruby-1.9.2-p180 :007 >             :email => "fred@mydomain.org",
ruby-1.9.2-p180 :008 >             :password => "badpass"
ruby-1.9.2-p180 :009?>         }
ruby-1.9.2-p180 :010?>     end
 => nil 
ruby-1.9.2-p180 :011 > user = User.new(user_fred)
 => #<User id: nil, email: "fred@mydomain.org", encrypted_password:     "$2a$10$IiEOEoSnXIrP7VJAQYckfOVXuzm7Y5ZGo20ayLpSkHhz...", reset_password_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, first_name: "Fred", last_name: "Flinstone", username: "fflint", dob: "1986-06-01", pb_user_account_id: nil> 
ruby-1.9.2-p180 :012 > user.create_pushbroom_user_data
=> #<Pushbroom::UserAccount id: nil, created_by: nil, created_at: nil, updated_by: nil,     updated_at: nil, admin_notes: nil, enabled: true, username: "fflint", password_hash: "blah     blah", user_id: nil, prefStore: nil, accepted_tos: nil, do_not_contact: nil> 
ruby-1.9.2-p180 :013 > user.pb_user_account.user_roles
 => [#<Pushbroom::UserRole id: 1, created_by: "script", created_at: "2008-11-10     12:10:44", updated_by: "script", updated_at: "2008-11-10 12:10:44", admin_notes: "", name:     "user", description: "Generic User Role", conditional: false>] 
ruby-1.9.2-p180 :014 > user.save!
NoMethodError: undefined method `relation' for nil:NilClass
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activesupport-    3.0.5/lib/active_support/whiny_nil.rb:48:in `method_missing'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/arel-    2.0.9/lib/arel/insert_manager.rb:22:in `insert'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/arel-    2.0.9/lib/arel/crud.rb:26:in `insert'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-    3.0.5/lib/active_record/associations/has_and_belongs_to_many_association.rb:76:in     `insert_record'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-    3.0.5/lib/active_record/associations/association_proxy.rb:151:in `send'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:306:in `block in     save_collection_association'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_collection.rb:431:in `block in     method_missing'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `block in method_missing'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `each'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `method_missing'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_collection.rb:431:in `method_missing'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:297:in `save_collection_association'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:163:in `block in add_autosave_association_callbacks'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:415:in `_run_create_callbacks'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:281:in `create'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/persistence.rb:246:in `create_or_update'
... 18 levels...
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:277:in `create_or_update'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/persistence.rb:56:in `save!'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/validations.rb:49:in `save!'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:245:in `block in save!'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-    3.0.5/lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in     `transaction'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:207:in `transaction'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:245:in `save!'
  from (irb):14
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
  from /Users/gander/.rvm/gems/ruby-1.9.2-p180@sms2/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'ruby-1.9.2-p180 :015 > 

如果我删除角色分配,一切都只是桃色(查找、保存、破坏等),但是当我尝试保存角色的第二次时,所有内容都会因为这个消息而变得高高在上,坦率地说,我不明白。它知道它有角色,没有我可以告诉的 nil 对象。 . .基本上,如果我还没有秃顶,我会把头发拉出来; )

非常感谢对此的任何见解!

杰拉德

附:也在这里问过http://railsforum.com/viewtopic.php?id=43647如果找到,将重复答案。

【问题讨论】:

  • 请添加您的create_pushbroom_user_account 方法定义
  • 糟糕,在剪切和粘贴中迷路了。在代码中添加了备份。感谢收看。

标签: ruby-on-rails-3 has-and-belongs-to-many


【解决方案1】:

在这件事上把自己弄傻了 4 天后,我终于发现了问题:Rails (habtm) 没有能力确定用于外部关系表的数据库。我也找到了答案,它甚至没有臭味!这里有一个完整的线程:http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/c5655d0442039ccd

答案? has_many :through - 我从来没有仔细研究过的东西,但它实际上是一个非常好的功能(即使在其他情况下也是如此)。

基本上这只允许我创建一个代表关系的模型类。因为我有一个模型类,所以我可以明确指定要连接的数据库。

为了后代,这里是代码:

class Pushbroom::UsersRolesRelationship < ActiveRecord::Base

  establish_connection :pushbroom
  set_table_name :users_roles

  belongs_to :user_account
  belongs_to :user_role
end

class Pushbroom::UserAccount < ActiveRecord::Base

  establish_connection :pushbroom
  set_table_name :user_account
  set_primary_key :id

  has_many :users_roles_relationships
  has_many :user_roles, :through => :users_roles_relationships, :source => :user_role
end

class Pushbroom::UserRole < ActiveRecord::Base

  establish_connection :pushbroom
  set_table_name :user_role
  set_primary_key :id

  has_many :users_roles_relationships
  has_many :user_accounts, :through => :users_roles_relationships, :source => :user_account
end

这样使用:

def add_subscription_plan_roles_to_pb_user_account(pb_user_account)
    roles_granted = pb_user_account.user.subscriptions.first.subscription_plan.roles_granted
    pb_user_account.user_roles = roles_granted
end

非常感谢大家帮助我让这列火车再次行驶!我所有的测试都通过了,它似乎正在工作,但如果你发现有问题,请告诉我。

谢谢! 杰拉德

【讨论】:

    【解决方案2】:

    尝试手动保存在不同位置创建的对象,例如create_pushbroom_user_account(pb_user) 方法。过去我在依赖“自动保存”系统时遇到过一些问题。

    【讨论】:

    • 我一直尝试保存每一步,在我到达 user_roles 之前它是 100% 成功的。无论我如何分配它们,即使是直接进行 SQL 插入(connection.execute/insert/etc),它也会出现同样的错误。这几乎是关系的配置方式,但我已经检查了 100 次,这不是火箭科学:/感谢您的评论!
    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2018-01-29
    • 2017-10-22
    • 2015-07-04
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多