Ruby/Rails:访问实例变量数组中的一个变量。每个变量都会导致Ruby解释器崩溃

Ruby/Rails:访问实例变量数组中的一个变量。每个变量都会导致Ruby解释器崩溃,ruby,ruby-on-rails-3,rvm,ruby-1.9.2,Ruby,Ruby On Rails 3,Rvm,Ruby 1.9.2,我写了一个mixin(基于我在博客上读到的东西),它似乎引起了一个问题 这是该项目的链接:(或者给我发电子邮件:aaron.a。ashworth@gmail.com我会给它发电子邮件)我已经尽我所能把它去掉了,这样它仍然会引起问题。要查看的关键文件有: /lib/user_role.rb (near line 11) /app/views/customers/index.html.erb (near line 16) /app/controllers/customers_controller.r

我写了一个mixin(基于我在博客上读到的东西),它似乎引起了一个问题

这是该项目的链接:(或者给我发电子邮件:aaron.a。ashworth@gmail.com我会给它发电子邮件)我已经尽我所能把它去掉了,这样它仍然会引起问题。要查看的关键文件有:

/lib/user_role.rb (near line 11)
/app/views/customers/index.html.erb (near line 16)
/app/controllers/customers_controller.rb (near line 47)
我也会在这里布置重要的内容:

/lib/user\u role.rb:

module UserRole
    def self.included(base)
      base.has_one :user, :as => :user_role, :autosave => true
      base.validate :user_must_be_valid
      base.alias_method_chain :user, :autobuild
      base.extend ClassMethods
      base.define_user_accessors
    end

    def user_with_autobuild
      user_without_autobuild || build_user
    end

    def method_missing(meth, *args, &blk)
      user.send(meth, *args, &blk)
    rescue NoMethodError
      super
    end

    module ClassMethods
      def define_user_accessors
        all_attributes = User.content_columns.map(&:name) + ["password", "password_confirmation"]
        ignored_attributes = ["created_at", "updated_at", "user_role_type"]
        attributes_to_delegate = all_attributes - ignored_attributes
        attributes_to_delegate.each do |attrib|
          class_eval <<-RUBY
            def #{attrib}
              user.#{attrib}
            end

            def #{attrib}=(value)
              self.user.#{attrib} = value
            end

            def #{attrib}?
              self.user.#{attrib}?
            end
          RUBY
        end
      end
    end

  protected
    def user_must_be_valid
      Logger.new(STDOUT).info('calling user_must_be_valid')
      unless user.valid?
        user.errors.each do |attr, message|
          errors.add(attr, message)
        end
      end
    end
  end
...
<% @customers.each do |customer| %>
  <tr>
    <td><%= customer.account_id %></td>
...
访问
客户
会导致问题。我可以对
@客户做任何事
但是,当我试图访问
客户..
甚至
@customers[0]…
时,我就遇到了一个问题

生产步骤

1) 解压缩文件后,进入终端的根目录并运行以下命令:

bundle install
bundle exec rake db:drop
bundle exec rake db:migrate
rails s
2) 打开浏览器至
localhost:3000/客户
,然后单击
新客户

3) 按如下方式填写您看到的表格:

Account: 3
First Name: First
Last Name: Last
Email: first.last@domain.com
Password: 1234
Password confirmation: 1234
4) 单击
创建客户
按钮

预期行为

您应该被重定向到
localhost:3000/customers/1

当前行为

当您收到以下消息时,Web服务器崩溃:

~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17b356) [0x7fef4a97e356]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1713ee) [0x7fef4a9743ee]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x177243) [0x7fef4a97a243]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_vm_invoke_proc+0x9f) [0x7fef4a97b08f]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17b644) [0x7fef4a97e644]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1713ee) [0x7fef4a9743ee]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x177243) [0x7fef4a97a243]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1784f4) [0x7fef4a97b4f4]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x178cb5) [0x7fef4a97bcb5]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17b50d) [0x7fef4a97e50d]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1713ee) [0x7fef4a9743ee]

[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
重新运行Web服务器并转到
localhost:3000/customers
有时会出现不同的错误。分段错误,它抱怨
/lib/user\u角色。rb:11

环境

Ruby 1.9.2-p290
Rails 3.0.9
RVM 1.8.1
Ubuntu 11.04
编辑

Ruby 1.9.2-p290
Rails 3.0.9
RVM 1.8.1
Ubuntu 11.04
需要注意的是:如果您尝试使用在控制台中轰炸的相同代码,那么它看起来很好。例如:

(After entering rails c)
@customers = Customer.all
@customers.each do |customer|
  p customer.account_id
end
# this doesn't cause an error or crash.

@customer[0].first_name
=> "First"
如果删除:

def method_missing(meth, *args, &blk)
  customer.send(meth, *args, &blk)
rescue NoMethodError
  super
end


从lib目录中的x_角色文件中,它应该可以正常工作。另一方面,请查看控制器的继承资源和表单的简单表单。

一旦这些资源可用,我将在这篇文章中添加赏金。我知道我向社区提出了很多要求,但我所在的地区没有地毯,也没有任何我认识的有ruby经验的人。如有任何帮助,我们将不胜感激。升级到Lion后,您是否正在使用OS/X Lion和编译的Ruby?在这种情况下,这可能是导致问题的原因(如果这确实是您的问题,我将发布有关如何解决问题的链接)。此外,我已尝试下载您的示例代码,以查看它是否在我的计算机上工作,但无法从您的主机获取链接电子邮件。你能把它贴到别的地方吗?我们可以直接从那里下载?谢谢你的机器有内存问题吗?我怀疑,但它可能值得检查(memtest?),只是为了确定。@arikfi,我正在使用Ubuntu11.04。如果你可以给我发封邮件(不要因为匿名而发你的邮件,我的是:aaron.a。ashworth@gmail.com)我会发电子邮件给你。我希望你能将文件附加到那个可怕的下载站点上,所以帖子和元工作环境都很糟糕。Matthew,立即解决我的问题的是一个gem,它让我拥有一个
用户
模型,允许
客户
模型从
用户
派生而来,而无需将
用户
客户
放在同一个表中(即STI)但是仍然能够访问像
@customer.login
这样的东西,而不是
@customer.user.login
。Matthew,我想这解决了我的问题。我打算在这个周末做更多的测试,但我想这就是解决我问题的方法。你可以将共享代码放入一个模块中,然后将该模块包含到你的客户和用户模型中。是的,我正在考虑制作一个gem,当你可以在一个模型中添加acts作为bleh时,它会自动连接起来。马特,你收到赏金了吗?它已从我的代表中删除,但看起来您的代表没有增加200。@DJTripleThreat奇怪的是,我没有,这可能是一个错误