# Authlogic and NameError "undefined local variable or method \`login'"

**URL:** <https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245>\
**Category:** Rails\
**Created:** [April 2, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245 "2010-04-02T22:54:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 2, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245/1 "2010-04-02T22:54:07Z")

</div>

Hi!  
I am using authlogic and I have a User model (with act\_as\_authentic)  
without the “login” field, since I use “email” as the user name. As it  
says in the authlogic documentatation, that should be the way it works  
when there is no login field.

When I create new users manually via the User -\> new form, everythings  
works fine.

But:

Now I’d like to add user accounts as a side effect when anonymous  
users fill in a form (actually it’s the “new” form for an entity  
called SearchSubscription).

Now, in my SearchSubscription class, I have this “before\_create  
:assign\_user” callback:

def assign\_user  
if self.user\_id.nil?  
# find user with the given email address. If not present, create  
new user.  
user = User.find(:first, :conditions =\> {:email =\> self.email})  
if !user.nil?  
self.user\_id = user.id  
else  
# User not found, so create one  
randompassword =  
Base64.encode64(Digest::SHA1.digest("#{rand(1\<\<64)}/#{Time.now.to\_f}/#{Process.pid}/#{login}"))[0…7]  
user = User.create(:email =\> self.email, :password =\>  
randompassword, :location =\> self.location)  
if user.id  
self.user\_id = user.id  
else  
flash[:warning] = “Could not create user account”  
end  
end  
end  
end

This works well for email addresses that have a user entry. But  
whenever I try to create a new SearchSubscription with an unknown  
email address, I get the following error:

NameError (undefined local variable or method `login' for #<SearchSubscription:0x104099750>): app/models/search_subscription.rb:26:in`assign\_user’  
app/controllers/search\_subscriptions\_controller.rb:21:in `create’

I have the impression that somewhere, the “User” class requires the  
login. But I don’t want to maintain a login field, since I want to use  
the email address for that.

Any ideas?

Thanks!

Marian

---

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 3, 2010, 8:55am UTC](https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245/2 "2010-04-03T08:55:09Z")

</div>

On 2 April 2010 13:18, Marian S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> user = User.find(:first, :conditions => {:email => self.email})
> if !user.nil?
> self.user_id = user.id
> else
> # User not found, so create one
> randompassword =
> 
> ```
> 
> Base64.encode64(Digest::SHA1.digest(“#{rand(1\<\<64)}/#{Time.now.to\_f}/#{Process.pid}/#{login}”))[0…7]

What is that login doing in the line above I wonder?  
I am not quite sure what the [0…7] is for either.

> This works well for email addresses that have a user entry. But  
> whenever I try to create a new SearchSubscription with an unknown  
> email address, I get the following error:
> 
> NameError (undefined local variable or method `login’ for  
> #SearchSubscription:0x104099750):

The clue is in the error message, it is SearchSubscription that it is  
complaining about, not User.

Colin

---

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 3, 2010, 10:34am UTC](https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245/3 "2010-04-03T10:34:05Z")

</div>

> > Base64.encode64(Digest::SHA1.digest("#{rand(1\<\<64)}/#{Time.now.to\_f}/#{Process.pid}/#{login}"))[0…7]
> 
> What is that login doing in the line above I wonder?  
> I am not quite sure what the [0…7] is for either.

Stupid me. I took that snippet some web page and didn’t even notice  
that it uses a variable I don’t have. ☹

The [0…7] might be another way of doing a substring.

> > NameError (undefined local variable or method `login’ for  
> > #SearchSubscription:0x104099750):
> 
> The clue is in the error message, it is SearchSubscription that it is  
> complaining about, not User.

Colin, thank you very much! I replaced #{login} with #{self.email} and  
now it works.

Marian

---

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 3, 2010, 10:36am UTC](https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245/4 "2010-04-03T10:36:26Z")

</div>

> > Base64.encode64(Digest::SHA1.digest("#{rand(1\<\<64)}/#{Time.now.to\_f}/#{Process.pid}/#{login}"))[0…7]
> 
> What is that login doing in the line above I wonder?  
> I am not quite sure what the [0…7] is for either.

Stupid me. I took that snippet some web page and didn’t even notice  
that it uses a variable I don’t have. ☹

The [0…7] might be another way of doing a substring.

> > NameError (undefined local variable or method `login’ for  
> > #SearchSubscription:0x104099750):
> 
> The clue is in the error message, it is SearchSubscription that it is  
> complaining about, not User.

Colin, thank you very much! I replaced #{login} with #{self.email} and  
now it works.

Marian

---

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 3, 2010, 10:37am UTC](https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245/5 "2010-04-03T10:37:01Z")

</div>

> > Base64.encode64(Digest::SHA1.digest("#{rand(1\<\<64)}/#{Time.now.to\_f}/#{Process.pid}/#{login}"))[0…7]
> 
> What is that login doing in the line above I wonder?  
> I am not quite sure what the [0…7] is for either.

Stupid me. I took that snippet some web page and didn’t even notice  
that it uses a variable I don’t have. ☹

The [0…7] might be another way of doing a substring. Is it not?

> > NameError (undefined local variable or method `login’ for  
> > #SearchSubscription:0x104099750):
> 
> The clue is in the error message, it is SearchSubscription that it is  
> complaining about, not User.

Colin, thank you very much! I replaced #{login} with #{self.email} and  
now it works.

Marian

---

<div class="post-metadata">

**Author:** ![mari\_an](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7ea924/32.png) [@mari\_an](https://www.ruby-forum.com/u/mari_an)\
**Post date:** [April 3, 2010, 2:52pm UTC](https://www.ruby-forum.com/t/authlogic-and-nameerror-undefined-local-variable-or-method-login/186245/6 "2010-04-03T14:52:46Z")

</div>

On 3 April 2010 09:32, Marian S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Base64.encode64(Digest::SHA1.digest(“#{rand(1\<\<64)}/#{Time.now.to\_f}/#{Process.pid}/#{login}”))[0…7]
> > 
> > What is that login doing in the line above I wonder?  
> > I am not quite sure what the [0…7] is for either.
> 
> Stupid me. I took that snippet some web page and didn’t even notice  
> that it uses a variable I don’t have. ☹
> 
> The [0…7] might be another way of doing a substring.

Yes, I realised that just after I posted. It is not a syntax that I  
use regularly.

Colin
