【问题标题】:Rails 3.1 really weird issues happening randomlyRails 3.1 随机发生的非常奇怪的问题
【发布时间】:2023-03-22 09:47:01
【问题描述】:

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

它确实经常在服务器启动后发生,而且大多数时候,如果不是每次,它是关于一个未定义的方法实际上已定义

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

例如我得到:

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:=链接到发布路径(comment.publication,:anchor =>“comment-#{comment.id}”)做 4:=链接到评论摘录(评论),评论链接(评论),:class=>“评论摘录” 5:\- 6:=链接到评论.作者.用户名,评论.作者 7:\- => 8: - 绑定.pry 9: = link_to truncate(comment.publication.title, :length => 30), comment.publication, :class=> "category-font #{comment.publication.color_class}" [1] 撬(#>)>comment.publication.color_class NoMethodError:未定义的方法“color_class”用于# 来自/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] 撬(#>)>comment.publication.category NoMethodError:未定义的方法“类别”# 来自/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] 撬(#>)>comment.publication.id => 139 [4] 撬(#>)>comment.publication == Publication.find(139) => 假的 [5] 撬(#>)> Publication.find(139).color_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]

有什么想法吗?

【问题讨论】:

  • 假设PublicationActiveRecord 模型,您不应该使用type,除非它是单表继承的一部分。你是吗?
  • @Chowlett 实际上我有出版物 belongs_to :type, :foreign_key => "publication_type_id", :class_name => "PublicationType"。我这样做只是为了方便,但会尝试在任何地方使用publication_type,看看它是否能防止这个问题。无论如何,我遇到了类似的问题(请参阅帖子编辑),但与此“类型”字段无关。

标签: ruby-on-rails


【解决方案1】:

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

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

【讨论】:

  • 我尝试不通过在任何地方使用“publication_type”来覆盖“type”,但错误仍然存​​在......
猜你喜欢
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多