views:

220

answers:

2

This issue is really losing me.

I have a model for authentication : user. I have a model for comments : comment. I have a model that is commentable : weburl.

Weburl 1..* Comment Weburl *..1 User Comment *..1 User

The issue I have is not testable (my tests are all passing), and does not happen all the time. Usually it happens the second time I generate the same controller action.

Issue is my user associated to my comment, and retrieve through comment.user become NOT EQUAL to current_user, even if the attributes are the same. For example :

(comment.user.login == current_user.login == User.find(1).login)
     ==> True
(comment.user.id)
     ==> Unknown in the current context
(comment.user.class == User == current_user.class)
     ==> True

I doubled check the relationships belongs_to and has_one has_many in my models

My guess is that is something to do with caching, I'm experiencing this on my development environment, but not in my automated tests nor in production.

I'd like to understand what is the issue.

Thanks

EDIT Adding my development.rb init file

config.cache_classes = false                                    #Set to true for prod
config.whiny_nils = true
config.action_controller.consider_all_requests_local = true     #Set to false for prod
config.action_view.debug_rjs                         = true
config.action_controller.perform_caching             = false    #Set to true for prod

EDIT #2 If config.cache_classes is true, the error is not happenning

Doc says : # In the development environment your application's code is reloaded on every request. This slows down response time but is perfect for development since you dont have to restart the webserver when you make code changes.

found the following ticket, might be interrelated http://dev.rubyonrails.org/ticket/10722 Also, issue happens with Mongrel and WebRick.

+1  A: 

I think what you mean from your question is: why doesn't this work: comment.user == current_user.

When you compare two Ruby objects to each other i.e. comment.user == current_user you are comparing their references, since they are two unique objects and sit in different areas of memory this is the correct behavior.

Instead you'll have to compare some other unique field like login or id.

Lolindrath
This make sense except, why wouldn't I be able to call comment.user.id ? And why would this behaviour be any different between prod and dev?Finally, I was under the impression that I could use the == operator to do instance comparison... not sure how much of my code is impacted by this...
Tested by replacing == by .eql? (which compares type and value) with no success
Ifconfig.cache_classes is true, the error is not happenningDoc says :# In the development environment your application's code is reloaded on every request. This slows down response time but is perfect for development since you dont have to restart the webserver when you make code changes
Really, I think this is the answer. The object retrieved from a db is not the same as the object saved to it. It just has the same attributes. Object-relational systems like rails can be confusing in this way.
Joe Soul-bringer
A: 

What's exactly the error thrown here: (comment.user.id)? If you can't access the User object from the Comment object, you might be missing something in the model. I'm not close to a console to verify this, though

Srdjan Pejic
I triple checked my comment and user models, they are valid. .user method is correctly added, it's its content which doesn't work