Rails 3.1真正奇怪的问题随机发生

时间:2011-12-08 14:24:40

标签: ruby-on-rails

我正在研究一些Rails 3.1中型应用程序,目前我在启动本地服务器后随机发生了一些奇怪的问题

它经常在服务器启动后发生,并且大多数情况下,如果不是每次,都是关于未定义的方法实际定义的

如果我在应用程序崩溃之前放置了一些 binding.pry 调试器,我尝试使用自己的方法我得到了预期结果(没有崩溃)。如果我离开调试控制台,服务器就会恢复正常。

例如我得到:

NoMethodError (undefined method `type=' for #<Publication:0xb398f180>): app/controllers/publications_controller.rb:115:in `new'

如果我去那个控制器并添加 binding.pry

@publication = current_user.publications.new
binding.pry
@publication.type = type

点击刷新,在控制台中输入

Publication.new.type = PublicationType.first

然后离开控制台,服务器恢复正常。

这似乎只发生在我的本地环境中。在部署到生产或运行规范之后,我从未遇到过这样的问题。

因为我是唯一没有那么糟糕的人,但很快其他开发人员就会在代码库上工作,所以这将是一个实际的问题。

编辑:

我今天遇到了另一个错误:

undefined method `color_class' for #<Publication:0xb39e44f0>

= link_to truncate(comment.publication.title, :length => 30), comment.publication, :class => "category-font #{comment.publication.color_class}"

然后我刚刚完成了binding.pry技巧并输入了

comment.publication.color_class

然后离开控制台,一切都很顺利......

编辑2:

好了,现在变得更加奇怪......

我遇到了与上面相同的问题,即color_class问题。除了这个时候所描述的技巧不起作用,请参阅pry输出:

     3:     = link_to publication_path(comment.publication, :anchor => "comment-#{comment.id}") do
     4:       = link_to comment_excerpt(comment), comment_link(comment), :class => "comment_excerpt"
     5:       \-
     6:       = link_to comment.author.username, comment.author
     7:       \-
 =>  8:       - binding.pry
     9:       = link_to truncate(comment.publication.title, :length => 30), 

    comment.publication, :class => "category-font #{comment.publication.color_class}"
    [1] pry(#>)> comment.publication.color_class
    NoMethodError: undefined method `color_class' for #
    from /home/jerefrer/.rvm/gems/ruby-1.8.7-p352/gems/activemodel-3.1.0/lib/active_model/attribute_methods.rb:385:in `method_missing'
    [2] pry(#>)> comment.publication.category
    NoMethodError: undefined method `category' for #
    from /home/jerefrer/.rvm/gems/ruby-1.8.7-p352/gems/activemodel-3.1.0/lib/active_model/attribute_methods.rb:385:in `method_missing'
    [3] pry(#>)> comment.publication.id
    => 139
    [4] pry(#>)> comment.publication == Publication.find(139)
    => false
    [5] pry(#>)> Publication.find(139).color_class
    => "some-class"

这次我似乎无法找到任何技巧......只是不断得到同样的错误......

编辑3:

还有一个新的!

> Comment.includes(:publication => :author).order('created_at DESC').limit(10)
Hirb Error: Association named 'author' was not found; perhaps you misspelled it?
> Comment.order('created_at DESC').limit(10)
[is working]
> Comment.order('created_at DESC').limit(10).first.author
[is working]

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您使用的是单表继承,还是类型只是您定义的列?如果是这种情况,请注意,activerecord默认情况下认为type列是指定层次关系中子类的名称,因此如果您没有实现,则可能存在冲突。 如果您确实希望将该属性称为类型,则应覆盖Base.inheritance_column

更多信息:http://code.alexreisner.com/articles/single-table-inheritance-in-rails.htmlhttp://api.rubyonrails.org/classes/ActiveRecord/Base.html(单表继承部分)