ruby on rails - Model/ActiveRecord not saving new data -
i have confirmed method works. takes email controller , changes email of specific user.
however never saves data. pass wrong email format , returns false if pass correct email method returns true means assigned new email , called safe.
# allows user change email address def change_email(newmail) address = emailveracity::address.new(newmail) if address.valid? self.email = newmail self.save return true else return false end end
i checked logs first hints nothing is:
started post "/members/editmail" 127.0.0.1 @ 2013-04-25 17:33:44 +0200 processing memberscontroller#editmail html parameters: {"authenticity_token"=>"*****=", "mail"=>"*****@gmail.com"} ←[1m←[35muser load (1.0ms)←[0m select `users`.* `users` `users`.`id` = 1 limit 1 ←[1m←[36mcharacter load (0.0ms)←[0m ←[1mselect `characters`.* `characters` `characters`.`user_id` = 1←[0m ←[1m←[35m (0.0ms)←[0m begin ←[1m←[36muser exists (0.0ms)←[0m ←[1mselect 1 `users` (`users`.`email` = binary '*****@gmail.com' , `users`.`id` != 1) limit 1←[0m ←[1m←[35muser exists (0.0ms)←[0m select 1 `users` (`users`.`username` = binary '******' , `users`.`id` != 1) limit 1 ←[1m←[36m (0.0ms)←[0m ←[1mrollback←[0m redirected http://localhost:3000/members/1 completed 302 found in 10ms (activerecord: 1.0ms)
also make sense have method change attribute. since i'm using devise gem authentication can use current_user
variable retrieve user object logged in user , call current_user.email = newmail; current_user.save
in controller.
self.save!
throw exception when not saved.
also, might not right:
self.save return true
self.save returns true or false according if saved or not. might want rid of return true
, let return value 1 returned self.save
self
keyword not needed in context, neither return
keywords. equivalent code:
# allows user change email address def change_email(newmail) address = emailveracity::address.new(newmail) if address.valid? self.email = newmail save true else false end end
that equivalent to
# allows user change email address def change_email(newmail) address = emailveracity::address.new(newmail) if address.valid? self.email = newmail save end address.valid? end
which should not want.