Rails how to update a column after saving?
what you want is a callback. You can create an after_save callback on your Konkurrancer model, which triggers after the save() method is called for that model.
For example:
class Konkurrancer < ActiveRecord::Base
after_save :do_foobar
private
def do_foobar
rating_score = self.rating_score
ratings = self.ratings
rating = (rating_score/ratings)
self.update_attributes(:ratings => rating)
end
end
[EDIT] You should use self, since the model you are editing is the model itself. Test it out, and apply necessary logic/implementation.
Have a look at this guide for more info.
Hope that helps!
Rails after_save method trying to update record over and over
If you need to call process_pages
only once after Creating a Page
record and not after Updating a Page record then I would suggest to use after_create
instead.
class Page < ActiveRecord::Base
require 'open-uri'
after_create :process_pages
def process_pages
self.html = open(self.url).read
self.save
end
end
With after_save :process_pages
, process_pages
method would be called every time you save a Page record. You are saving a Page record again within process_pages
method which triggers the after_save
callback and you start looping.
See this SO Question for difference between after_save and after_create.
You will understand better as to why you are going in loops.
How can i update another model's data before save of one model
You can try this code. For creating transaction from user call
current_user.transaction.new(transaction_params)
Now in model transaction.rb file
class Transaction < ApplicationRecord
belongs_to :user
before_save :check_and_update_balance
def check_and_update_balance
## since user or sender is already there no need of sender
receiver = User.find_by(email: user_email)
if user.has_sufficient_balance?(amount) and receiver.present?
user.update_attributes(balance: balance - amount)
receiver.update_attributes(balance: balance + amount)
end
end
end
HOPE this Helps !!
Update a column on update of others columns properly
You can use changed_attributes
return unless (changed_attributes.keys & %w[currency price1 price2 price3 price4 price5 price6]).any?
self.prices_updated_at = Time.zone.now
Ruby on rails save a specific field in database
You can update a precise set of fields with update_attributes.
@house.update_attributes(:colour => newcolour)
You can update a single field with update_attribute.
@house.update_attribute(:colour, newcolour)
How to save another column updated time?
I would create a sub model of your Post
model. Lets call it PostRecord
.
So your Post
model will have a has_many
relationship with PostRecord
, and PostRecord
will belong to Post
class Post < ActiveRecord::Base
has_many :post_records
end
class PostRecord < ActiveRecord::Base
belongs_to :post
end
Your PostRecord
model will have:
post_id as type integer
read_at as type string
In your Post
model you should define a method like this:
def updated_read_at
self.post_record.create post_id: self.id, read_at: Time.now if read_status_changed?
end
More information on the changed? method
Update a column, all rows
Try this:
Foo.update_all(some_column: "bar")
This will generate SQL query to database:
UPDATE "foos" SET "some_column" = "bar";
Related Topics
Sidekiq: Ensure All Jobs on the Queue Are Unique
Is There a Shorter Way to Require a File in the Same Directory in Ruby
Ruby Dsl (Domain Specific Language) Repositories, Examples
How to Use Redis in a Multi-Threaded Rails Environment? (Puma/Sidekiq)
Ruby: Proc.New { 'Waffles' } VS. Proc { 'Waffles' }
How to Set the Actionmailer Default_Url_Options's :Host Dynamically to the Request's Hostname
How to Remove a Substring After a Certain Character in a String Using Ruby
Edit Each Line in a File in Ruby
Differences Between Proc and Lambda
Failed to Build Gem Native Extension
Error to Installing SASS with Gem in Windows 8.1
Put Haml Tags Inside Link_To Helper
What Is the Best/How to Validate an Email Address in Ruby
Redis or Mongo for Determining If a Number Falls Within Ranges