When is_a? fails and a Foo isn't a Foo

This is a weird one. It would be one thing if is_a? failed consistently
but if I refresh the page the second attempt always works.

I have the following code that works fine every time:

@node = Node.find(params[:id])
@node.is_a?(Node) # => always will return true

However I recently added an accounts layer to my code and now the query
looks like this:

class Account
has_many :nodes
end

@node = current_account.nodes.find(params[:id])
@node.class # => Node
@node.is_a?(Node) # => false on first request, true on subsequent ones

The sql query generated from the above query looks fine:
SELECT “nodes”.* FROM “nodes” WHERE (“nodes”.account_id = 1) AND
(“nodes”.“id” = 42) LIMIT 1

But something funny is going on because not even the following works:
@node = current_account.nodes.find(params[:id])
@node # => #Node:0x00000104cea108
@node.class # => Node
@node.class == Node # => false

I appreciate any input. Been banging my head on this one for a while
now and making no progress.

Could it be something that has to do with ARel and instead of getting an
object as a result you get an “active relation”/proxy?

More info to work off. When I swap out rails shorthand query of

current_account.nodes.find(params[:id])

with

Node.find(params[:id],
:conditions => [‘account_id = ?’, current_account.id])

everything works fine.

That was my first thought but it doesn’t appear to be the case. If it
were the class of the returned object should be ActiveRecord::Relation
instead of Node.

On Feb 6, 9:48pm, Ryan M. [email protected] wrote:

That was my first thought but it doesn’t appear to be the case. If it
were the class of the returned object should be ActiveRecord::Relation
instead of Node.

It’s worth noting that class is a method like any other in ruby and
can easily be overwritten. I don’t think this is what is happening
here.
To me, this smells like a code reloading issue. If most of your code
is getting reloading, but Account isn’t (eg because it’s in a plugin
or something like that), then you can have multiple versions of the
same class in memory. Node refers to the most current one, but the
Account class may be holding onto a previous version, in which case
user.nodes will use that version.
If changing the cache_classes setting (and restarting the app) makes a
difference then your problem is probably a code reloading thing. The
only unusual thing is that with code reloading things, normally it’s
the first request that works and subsequent ones that are weird, not
the other way around.

Fred

Not sure if it makes a difference, but the current_account instance is
loaded in a before_filter. Could that have anything to do with this?

I think you nailed it Fred. Restarting the app with cache_classes on
fixed the error.

Both the Account and Node models are simple classes though, so I’m not
sure where the reloading is becoming a problem.

Why wouldn’t simply restarting the server with cache_classes off fix
something like this as long as I don’t modify any code? I tried and the
only thing that got rid of the error was to turn off cache_classes.

In case anyone else runs into this issue here’s the solution:
https://rails.lighthouseapp.com/projects/8994/tickets/5497-default-scope-and-class-reloading

Looks like it’s been fixed as of Rails 3.0.2

On Feb 8, 6:34pm, Ryan M. [email protected] wrote:

Not sure if it makes a difference, but the current_account instance is
loaded in a before_filter. Could that have anything to do with this?

don’t think so. Another thing that can cause this sort of issue is if
you use require to require parts of your app (rather than letting
rails do things for you)

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs