# Assigning values to a session? Ben Curtis's OpenID example?

**URL:** <https://www.ruby-forum.com/t/assigning-values-to-a-session-ben-curtiss-openid-example/105406>\
**Category:** Rails\
**Created:** [May 29, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/assigning-values-to-a-session-ben-curtiss-openid-example/105406 "2007-05-29T18:45:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bravedave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/46a35a/32.png) [@bravedave](https://www.ruby-forum.com/u/bravedave)\
**Post date:** [May 29, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/assigning-values-to-a-session-ben-curtiss-openid-example/105406/1 "2007-05-29T18:45:01Z")

</div>

I’m taking apart Ben Curtis’s OpenID / RESTful authentication ‘head  
start’ and trying to understand it enough to integrate it into my app.

# registration is a hash containing the valid sreg keys given

above

# use this to map them to fields of your user model

def assign\_registration\_attributes!(registration)  
{ :login =\> ‘nickname’, :email =\> ‘email’ }.each do |  
model\_attribute, registration\_attribute|  
unless registration[registration\_attribute].blank?  
@user.send("#{model\_attribute}=",  
registration[registration\_attribute])  
end  
end  
@user.save!  
end  
I’ve never seen .each used in this fashion? Why is he using the  
“.each” when he appears to be just operating on ONE record?  
What does he mean by “sreg” keys?  
What does he mean to “use this to map them to fields of your user  
model”?  
Could someone explain in ‘human words’ what is going on here?  
What do these variable mean/do? “model\_attribute,  
registration\_attribute”  
Thank you,  
David

---

<div class="post-metadata">

**Author:** ![bravedave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/46a35a/32.png) [@bravedave](https://www.ruby-forum.com/u/bravedave)\
**Post date:** [May 29, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/assigning-values-to-a-session-ben-curtiss-openid-example/105406/2 "2007-05-29T20:17:15Z")

</div>

BraveDave wrote:

> I’m taking apart Ben Curtis’s OpenID / RESTful authentication ‘head  
> start’ and trying to understand it enough to integrate it into my app.
> 
> # registration is a hash containing the valid sreg keys given
> 
> above
> 
> # use this to map them to fields of your user model
> 
> def assign\_registration\_attributes!(registration)  
> { :login =\> ‘nickname’, :email =\> ‘email’ }.each do |  
> model\_attribute, registration\_attribute|  
> unless registration[registration\_attribute].blank?  
> @user.send("#{model\_attribute}=",  
> registration[registration\_attribute])  
> end  
> end  
> @user.save!  
> end  
> I’ve never seen .each used in this fashion? Why is he using the  
> “.each” when he appears to be just operating on ONE record?  
> What does he mean by “sreg” keys?  
> What does he mean to “use this to map them to fields of your user  
> model”?

I changed the code as follows

{ :login =\> ‘nickname’, :email =\> ‘email’ }.each do |  
model\_attribute, registration\_attribute|  
puts “In this iteration we have model\_attribute = ‘#{model\_attribute}’  
and registration\_attribute = ‘#{registration\_attribute}’”  
end

Let’s see what irb comes up with:

# irb

Welcome to irb. Method suc performs User.current =  
User.find\_by\_user\_name(‘admin’)  
irb(main):001:0\>  
irb(main):002:0\* { :login =\> ‘nickname’, :email =\> ‘email’ }.each do  
|  
irb(main):003:1\* model\_attribute, registration\_attribute|  
irb(main):004:1\* puts “In this iteration we have model\_attribute =  
‘#{model\_attribute}’ and registration\_attribute =  
‘#{registration\_attribute}’”  
irb(main):005:1\> end  
In this iteration we have model\_attribute = ‘email’ and  
registration\_attribute = ‘email’  
In this iteration we have model\_attribute = ‘login’ and  
registration\_attribute = ‘nickname’  
=\> {:email=\>“email”, :login=\>“nickname”}

Ignore the last line; irb gives you the result of the last expression.  
You’re looking at a looping construct. Hope that explains a little.

Stephan
