【问题标题】:rails 4 - undefined method `valid?'rails 4 - 未定义的方法“有效吗?”
【发布时间】:2014-12-09 21:44:51
【问题描述】:

我最近将我的 rails 升级到 Rails 4

这个查询曾经有效:

@user = User.joins(:cars).where('cars.color' => "blue")

但是现在我收到了这个错误信息:

未定义的方法“有效吗?” #

有什么想法吗?

【问题讨论】:

    标签: sql ruby-on-rails join where


    【解决方案1】:

    查询User.joins(:cars).where('cars.color' => "blue") 返回一个ActiveRecord::Relation 实例。检查docs here

    如果你真的想对一个实例(User 类)进行操作,你必须从该集合中获取一个用户,例如:

    @user = User.joins(:cars).where('cars.color' => "blue").first
    

    ActiveRecord::Relation 不响应以下方法:.save、.update_attributes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      相关资源
      最近更新 更多