Questions tagged [update-attributes]
164 questions
270
votes
11 answers
Rails: #update_attribute vs #update_attributes
obj.update_attribute(:only_one_field, 'Some Value')
obj.update_attributes(field1: 'value', field2: 'value2', field3: 'value3')
Both of these will update an object without having to explicitly tell ActiveRecord to update.
Rails API…

thenengah
- 42,557
- 33
- 113
- 157
30
votes
3 answers
problem with passing booleans to update_attributes
I've got the following Model:
class GuestCatering < ActiveRecord::Base
# Validation
validates :name, :presence => true
validates :order_number, :presence => true
validates :orderable, :presence => true
end
But when I'll try to update an…

LeonS
- 2,684
- 2
- 31
- 36
29
votes
4 answers
Nested attribute update_attributes uses insert rather than update
I have a user and nested profile class as follows:
class User < ActiveRecord::Base
has_one :profile
attr_accessible :profile_attributes
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
…

Jason
- 555
- 1
- 4
- 8
29
votes
9 answers
Updating `User` attributes without requiring password
Right now, users can edit some their attributes without having to enter their password because my validations are set up like this:
validates :password, :presence =>true, :confirmation => true, :length => { :within => 6..40 }, :on =>…

steffi2392
- 1,345
- 3
- 19
- 19
25
votes
5 answers
How to "update_attributes" without executing "before_save"?
I have a before_save in my Message model defined like this:
class Message < ActiveRecord::Base
before_save lambda { foo(publisher); bar }
end
When I do:
my_message.update_attributes(:created_at => ...)
foo and bar are…

Misha Moroshko
- 166,356
- 226
- 505
- 746
18
votes
2 answers
rails update_attributes returns false when trying to update db values
hoping someone here can point me in the right direction.
I have a controller Update def running "update_attributes". Currently it returns false, with no error message. I'm fairly new to Ruby, but not to coding, and this has had me stumped for a…

steve
- 221
- 1
- 3
- 9
17
votes
2 answers
Is the Rails update_attributes method the best choice for doing an update of a model in the database?
def update
@album = Album.find(params[:id])
if @album.update_attributes(params[:album])
redirect_to(:action=>'list')
else
render(:action=>'edit')
end
end
A Rails 1.1.6 tutorial that I'm covering recommends using the…

pez_dispenser
- 4,394
- 7
- 37
- 47
12
votes
1 answer
Rails 3 - how to save (un)checked checkboxes?
I have in a form (form_tag) several checkboxes like this:
<%=check_box_tag 'model_name[column_name]', 1, (@data.model_name.column_name == 1 ? true : false)%>
And updating them like:
variable = ModelName.find(params[:id]) …

user984621
- 46,344
- 73
- 224
- 412
10
votes
6 answers
Is there a way to prevent serialized attributes in rails from getting updated even if there are not changes?
This is probably one of the things that all new users find out about Rails sooner or later. I just realized that rails is updating all fields with the serialize keyword, without checking if anything really changed inside. In a way that is the…

Tabrez
- 3,424
- 3
- 27
- 33
10
votes
5 answers
Rails: How to check if "update_attributes" is going to fail?
To check if buyer.save is going to fail I use buyer.valid?:
def create
@buyer = Buyer.new(params[:buyer])
if @buyer.valid?
my_update_database_method
@buyer.save
else
...
end
end
How could I check if update_attributes is going to…

Misha Moroshko
- 166,356
- 226
- 505
- 746
10
votes
1 answer
Finding which fields have been updated after calling update_attributes?
I'd like to know which fields have been updated after an update_attributes statement. I'm filtering the updatable parameters and deleting those that I don't want updated from the params[:model]. Now, some of the new updatable params might have the…

Oktav
- 2,143
- 2
- 20
- 33
9
votes
2 answers
Rails - How to manage nested attributes without using accepts_nested_attributes_for?
My problem is I've run into limitations of accepts_nested_attributes_for, so I need to figure out how to replicate that functionality on my own in order to have more flexibility. (See below for exactly what's hanging me up.) So my question is: What…

JoshDoody
- 417
- 1
- 4
- 14
7
votes
1 answer
Rails - update_attributes coming up against validations
So I've got a user model, with login, email address, password, password confirmation, name, avatar (picture), etc. There are validations on the first 5, basically stating that all 5 need to exist in order to create a new model.
However, this causes…

Jty.tan
- 808
- 9
- 25
6
votes
1 answer
validation_context & update_attributes
How can I specify validation_context with update_attributes ?
I can do that using 2 operations (without update_attributes):
my_model.attributes = { :name => '123', :description => '345' }
my_model.save(:context => :some_context)

maxs
- 427
- 1
- 6
- 15
6
votes
2 answers
What does `update_attribute` return if it fails?
I have following piece of code
@user = User.find(params[:id])
if (@user.activation_status == "active")
#some code here
@user.update_attribute('activation_status' ,'inactive') # Line 44
#send mail to user that his account is…

Salil
- 46,566
- 21
- 122
- 156