user3659968
user3659968

Reputation: 43

"RuntimeError unknown class:" when accessing result of a "joins" operation

When trying to access the result of a Model.joins(ActiveRecord::Relation) operation, an error occurs.

The models are as follows:

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

Log shows:

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):

Both, Device and AndroidDevice, are created via command rails g scaffold ...

I do not understand why AndroidDevice is an unknown class.

Upvotes: 4

Views: 4567

Answers (1)

Sergey Alekseev
Sergey Alekseev

Reputation: 12300

You can't use .joins with ActiveRecord::Relation as parameter.

Probably you want http://apidock.com/rails/ActiveRecord/SpawnMethods/merge functionality.

Upvotes: 4

Related Questions