Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > user.attributes[:email] and user.email

Reply
Thread Tools

user.attributes[:email] and user.email

 
 
tom@tmedhurst.co.uk
Guest
Posts: n/a
 
      05-26-2006
Hi

Could somebody explain to me why the following doesn't work:

@user = User.find(:first)
@user.attributes[:email] = params[:user][:email] # this does nothing!!
@user.save

but the following DOES work...

@user = User.find(:first)
@user.email = params[:user][:email] # this works like it should?!
@user.save

I only realized this when the @user.update_attributes(params[:user])
didn't seem to do anything. I was under the impression that the two
snippets above did the same thing?

Does anybody else have this problem with @user.update_attributes??

Thanks in advance

Tom Medhurst

 
Reply With Quote
 
 
 
 
loidee
Guest
Posts: n/a
 
      05-26-2006
the reason is because the @user has a variable defined as email but not
a variable of type array or hash, named attributes. I hope this helps,
if not let me know

 
Reply With Quote
 
 
 
 
tom@tmedhurst.co.uk
Guest
Posts: n/a
 
      05-26-2006
I probably should have mentioned that @user is a class that inherits
from ActiveRecord::Base.
so the attributes hash *should* be wired up the same as the
accessors..?
the @user.update_attributes was a line code-gen'ed by rails so why
wouldn't it work?

 
Reply With Quote
 
Codemonk
Guest
Posts: n/a
 
      05-26-2006
Because attributes creates a copy of the current values stored in a
hash, while when you assign the attribute directly you are changing the
value itself.

@user.attributes # This copies all of the current values of the @user
object into a hash
@user.attribute_name # is the actual value stored on the user object.

@user.attributes does NOT return a pointer to the underlying attributes
hash

 
Reply With Quote
 
loidee
Guest
Posts: n/a
 
      05-27-2006
i figured it was inherited from active record but i think codemonk
explains it quite well. i suppose the fact that there is a method
#update_attributes can be misleading and cause one to expect an actual
attributes hash

 
Reply With Quote
 
tom@tmedhurst.co.uk
Guest
Posts: n/a
 
      05-28-2006
thanks for the info!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments