Rails update_attributes without save?
I believe what you are looking for is assign_attributes
.
It's basically the same as update_attributes but it doesn't save the record:
class User < ActiveRecord::Base
attr_accessible :name
attr_accessible :name, :is_admin, :as => :admin
end
user = User.new
user.assign_attributes({ :name => 'Josh', :is_admin => true }) # Raises an ActiveModel::MassAssignmentSecurity::Error
user.assign_attributes({ :name => 'Bob'})
user.name # => "Bob"
user.is_admin? # => false
user.new_record? # => true
Rails: set params but do not save
You can use attributes=
to set the attributes but not save the record.
@user.attributes = hash
New attributes will be persisted in the database when the object is saved. See http://apidock.com/rails/ActiveRecord/AttributeAssignment/attributes
How to accomplish an update_attributes without actually saving the changes
Depending on the source of ActiveRecord::Persistence#update_attributes
# File activerecord/lib/active_record/persistence.rb, line 127
def update_attributes(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
self.attributes = attributes
save
end
end
you can assign attributes to your model by using
model.attributes = attributes
where attributes is a hash of model fields etc.
How to update_attributes without executing before_save?
In rails 3.1 you will use update_column.
Otherwise:
In general way, the most elegant way to bypass callbacks is the following:
class Message < ActiveRecord::Base
cattr_accessor :skip_callbacks
before_save lambda { foo(publisher); bar }, :unless => :skip_callbacks # let's say you do not want this callback to be triggered when you perform batch operations
end
Then, you can do:
Message.skip_callbacks = true # for multiple records
my_message.update_attributes(:created_at => ...)
Message.skip_callbacks = false # reset
Or, just for one record:
my_message.update_attributes(:created_at => ..., :skip_callbacks => true)
If you need it specifically for a Time
attribute, then touch
will do the trick as mentioned by @lucapette .
update_attributes is not saving values to database
The submit form action of reservation's 'new' view would call the 'create' method of the reservation controller to which you can pass your car_id as a parameter. Then in the submit method you can get the associated car by @car = Car.find(params[:car_id]) and try updating the car status
How to update attributes without validation
USE update_attribute instead of update_attributes
Updates a single attribute and saves the record without going through the normal validation procedure.
if a.update_attribute('state', a.state)
Note:- 'update_attribute' update only one attribute at a time from the code given in question i think it will work for you.
Rails: Update model attribute without invoking callbacks
Rails 3.1 introduced update_column
, which is the same as update_attribute
, but without triggering validations or callbacks:
http://apidock.com/rails/ActiveRecord/Persistence/update_column
update_attributes isn't saving to the database
After playing around with in the console I've found out that even if I change the attributes manually the attributes don't 'stick' after I exit the console.
So I'll enter console, change the users attributes, test them, and they'll be changed. If I exist and re-enter, THEN test them, they'll have reverted back to their default values.
This leads me to believe that the 'after_initialize' method within the user model which sets its default values is running after each save. I though that it would only run after the object had been saved for the first time alone but now I know it run each time it is saved.
after_initialize :default_values
def default_values
self.goal = "Cut"
self.measurement = "US"
self.bmr_formula = "katch"
self.fat_factor = 0.655
self.protein_factor = 1.25
self.deficit_pct = 0.10
self.target_bf_pct = 0.10
self.activity_factor = 1.3
end
Once I remove all these values and the after_initialize method, it saves permanently.
Rails: Data not getting saved with assign_attributes in before save
This got solved. Since, i was updating an existing record, assign_attribute wasnt updating attributes. Only new record will be updated accroding to this:
Rails: Why “collection=” doesn't update records with existing id?
Manually fail #update_attributes save in Rails
You can just check the condition on a before_save
callback in model foo.rb
and return false if you don't want to save it.
before_save :really_want_to_save?
private
def really_want_to_save?
conditional_says_yes ? true : false
end
If you want error message too, then
def really_want_to_save?
if conditional_says_yes
true
else
errors[:base] << "failed"
false
end
end
Related Topics
How to Replace Accented Latin Characters in Ruby
Ruby Multiline Block Without Do End
Ruby'S File.Open and the Need For F.Close
What's the Difference Between a Proc and a Lambda in Ruby
Using Net::Http.Get For an Https Url
Find Indices of Elements That Match a Given Condition
Rescue_From Actioncontroller::Routingerror in Rails 4
Rails 5: Activerecord or Query
Why Does String Interpolation Work in Ruby When There Are No Curly Braces
Pass Parameter by Reference' in Ruby
How to Use Ruby to Colorize the Text Output to a Terminal
Disable Activerecord For Rails 4