[LRUG] Clearance: problems with equivalence of #current_user

Frederick Cheung frederick.cheung at gmail.com
Tue Apr 21 08:57:45 PDT 2015


On 21 April 2015 at 16:30:00, Tom Armitage (tom at infovore.org) wrote:
User is indeed a subclass of ActiveRecord::Base, and current_user is a User, and yet include appears to fail magically.

I'm going to stick to testing on IDs, then, which works, and perhaps try to understand this another day.



Could this be an annoying development mode code reloading issue?

If current_user is an instance of the current User class but another_user is an instance of a since unloaded version of the user class then current_user == another_user will be false (at least according to a quick experiment) even if they did correspond to the same row in the database

The readme for clearance does look like they might lead you down that route since it stashes User as part of an initializer which it presumably continues to use post reloading of your code.

A simple test for this would be to compare current_user.class.object_id to the object_ids of the class of the things inside this array you are using.

Fred

t.

On Tue, Apr 21, 2015 at 4:10 PM, Tom Stuart <tom at codon.com> wrote:
On 21 Apr 2015, at 15:56, Stephen Best <bestie at gmail.com> wrote:
> To make Array#include? work as you expect for two different instances of the same user record you would have to redefine equality (User#==) in terms of the object values or maybe just the id.

If `User` is a subclass of `ActiveRecord::Base`, the `User#==` method should already do this: https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/core.rb#L377-L391

Cheers,
-Tom
_______________________________________________
Chat mailing list
Chat at lists.lrug.org
Archives: http://lists.lrug.org/pipermail/chat-lrug.org
Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org



--
Tom Armitage
http://infovore.org
07813 060578
_______________________________________________ 
Chat mailing list 
Chat at lists.lrug.org 
Archives: http://lists.lrug.org/pipermail/chat-lrug.org 
Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org 
List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20150421/029f2c72/attachment-0003.html>


More information about the Chat mailing list