authenticates_many is broken in Rails 3

124 views
Skip to first unread message

Joshua Partogi

unread,
Jul 2, 2010, 1:06:40 PM7/2/10
to auth...@googlegroups.com
Hi when adding authenticates_many to my Account model I am receiving
this error message:

undefined method `delete_if' for #<Proc:0xb577ebf0>

So my question is, does anybody know how to use the find_options
parameter in authenticates_many?

I tried like this with no success:
authenticates_many :user_sessions, :find_options => { :conditions => {
:account_id => self.object_id } }

Has anybody used this find_options before? Any pointers would be great.

Regards,
Joshua.

--
http://twitter.com/scrum8

Norsk.Firefox

unread,
Jul 5, 2010, 1:13:39 AM7/5/10
to Authlogic
Need this allot my self! Please someone, is there a fix for this?

Kirkconnell

unread,
Jul 8, 2010, 9:00:48 PM7/8/10
to Authlogic
After reading the source code of Authlogic I figured out that there's
a bug in the authenticates_many method. The problem is that if
no :find_options hash is provided, Authlogic will create a
named_scope, and then threat it as if it's a hash of find_options.
What I did to solve this was to give a fake :find_options scope hash.
The call in my Account model looked as follows:

authenticates_many :user_sessions, :find_options => { :limit => 1 }

Since we're only authenticating one record at a time, it doesn't
affect to add this scope to the conditions.

In both the new and create actions of the UserSessionsController
instead of using

@user_session = UserSession.new(params[:arg]

I loaded the Accounts model from a domain parameter and created the
new UserSession from there.

account = Account.find_by_domain(params[:domain])
@user_session = account.user_sessions.new(params[:arg])

After doing that my authentication worked fine. I hope you find this
helpful.

Regards,
Carlos Kirkconnell

Joshua Partogi

unread,
Jul 11, 2010, 8:04:07 AM7/11/10
to Authlogic
Thanks Kirk,

This is definitely useful. I will try it out.

Cheers.

Jake Paul

unread,
Aug 3, 2010, 2:02:43 PM8/3/10
to Authlogic
I modified authenticates_many to use the more Rails 3-ish scoped
method rather than scope(:find) (and simply commented out the
delete_if line because I'm not using a hash). The scoped method
returns a Relation that contains the constraint needed by
authenticates_many (e.g. "account_id = :id"), and with_scope in Rails
3 knows what to do with it. The only other modification I needed was
to Session::Scopes, because the search_for_record method is what calls
with_scope. Instead of passing the :find => {} options to with_scope,
you need to pass the relation.

Here's the commit on my GitHub fork:
http://github.com/jakepaul/authlogic/commit/44d3c8d58a0210121540c792e98b188cdff0ff11

It's not a complete (or backwards-compatible) solution, but it works.

Jake Paul
Reply all
Reply to author
Forward
0 new messages