" RuntimeError未知类:"当访问"加入"的结果时手术

时间:2014-05-21 09:17:15

标签: ruby-on-rails activerecord

尝试访问Model.joins(ActiveRecord::Relation)操作的结果时,会发生错误。

模型如下:

class Device < ActiveRecord::Base
    has_one :android_device

    def Device.find_devices_by_attributes(device_params)
        android_devices = AndroidDevice.find_android_devices_by_attributes(device_params[:android_device_attributes])

        if (!android_devices.nil?) && (!android_devices.empty?)
            devices_android_device = Device.joins(android_devices)   <--- this is the line the error occures on.
        end
    end
end

class AndroidDevice < ActiveRecord::Base
    belongs_to :device

    def AndroidDevice.find_android_devices_by_attributes (android_params)
        android_devices = AndroidDevice.where("android_id = ?", android_params[:android_id])
    end
end

日志显示:

2014-05-21T08:46:14.719594+00:00 app[web.1]: Completed 500 Internal Server Error in 87ms
2014-05-21T08:46:14.721376+00:00 app[web.1]: RuntimeError (unknown class: AndroidDevice):

DeviceAndroidDevice都是通过命令rails g scaffold ...

创建的

我不明白为什么AndroidDevice是一个未知的类。

1 个答案:

答案 0 :(得分:2)

您不能将.joins与ActiveRecord :: Relation一起用作参数。

您可能需要http://apidock.com/rails/ActiveRecord/SpawnMethods/merge功能。