ruby on rails - Updating database, what options do I have? -
i'm beginner in rails, , want know how can update database?(i'd know possible options it).
i got simple table called users, got :name , :email attributes.
as read can update :email by:
user.find_by(:id => 1).update_attribute email, "sample@foo.bar"
<- okuser.find_by(:id => 1).update_attributes :email => "sample@foo.bar"
<- return me false
and there way update by:
@foo = user.find_by(:id => 1) foo.email = "sample@foo.bar" foo.save
update_attribute
skips validations. update_attributes
returning false
since it's not passing validation. double check user
model validations , make sure under attr_accessible
you've added email
:
class user < activerecord::base attr_accessible :email, :name # etc end
also find
don't have specify attribute, use integer:
@user = user.find(24) # find user id of 24 @user.email = "sample@foo.bar" @user.save # or update_attributes can update multiple attributes @ once: @user.update_attributes(email: "sample@foo.bar", name: "bob") # or update_attribute skips validations , can update 1 # attribute @ time. used specific situations: @user.update_attribute(:email, "sample@foo.bar")
Comments
Post a Comment