ActiveRecord::Base descendant "loses" the "id" method?


How can an object that self-identifies as ActiveRecord::Base _not_ have the id method?

I'm going to be maybe too obvious here, but according to Occam that would be the right solution :wink:

Maybe your table has a custom key field? I'm going to risk and say that in this piece of output there is a "UniqueID" that looks suspicious to me.

"Merge2"=>nil, "Ad2"=>nil, "UniqueID"=>64867, "ConnectTime"=>nil,
Of course it could be a totally different issue, but i thought it was worth to give it a try. Have you tried to do "puts Target.primary_key" just in case?


Javier Ramirez

object.instance_variable_set("@attributes", record)
puts "object has id: #{object.methods.include?('id')}"
puts "object responds to id: #{object.respond_to?('id')}"
puts "object class: #{object.class}"
puts "object is a AR: #{object.is_a?(ActiveRecord::Base)}"

Just in case I created a model with a custom PK, and I run the same code, and I got true in the three cases you are having a false, so I definitely rule out the thing about the custom PK (anyway, after your fist reply I already thought it was not because of that, but you know.. seeing is believing :wink: )

Guess your model is nothing fancy. I mean, my model for testing was just
class Apk < ActiveRecord::Base
  set_primary_key :my_id

So, I don't know.. maybe you are using some mixins or any plugins that make some (black) magic over AR:Base?.

Do you know what is even weirdest? I was thinking every object responds to "id" (even if is deprecated and object_id should be used), so if it weren't a AR object and if the id method was not overwritten, It should always respond to "id"

I'm trying here but I'm unable to replicate your problem :frowning:

good luck!


IF I store this datagrid object in a hash, then the subsequent finds fail in the way that I've described above.

mmmm.. well.. being so and taking into account you said yesterday that you can only access attribute variables, it looks as when the resultset is not mapped to the AR object (not sure if the AR method for this was called "instantiate"). For example, that's what you get when you do a ActiveRecord::Base.connection.select_all(). You get an array of hashes, but no AR objects.

Of course this is not what's really happening, because you were getting "Target" as the class and it said it's a AR::Base object... but still it looks to me like that, as if you were getting a plain hashes array as a result.. why? no clue