Ruby before_validation triggers infinite loop of call back
You don't need self.save!
there: you are already inside the transaction, so just do whatever you want to do and let Rails save the record once you are done.
Sidenotes:
Product.where(:id => self.id).update_all(attributes)
probably could be rewritten asproducts.update_all(attributes)
(having the associationthis_model has_many :products
self.attributes = attributes
is redundant unlessattributes
is an instance method.
Ruby on Rails - after_validation if valid?
The failing validations add to the errors for the record, so you could just check:
return false unless self.errors.empty?
Rails 4: after_update callback leads to endless loop
Try this:
User.rb
attr_accessor :phone_checked
after_update :check_phone, :unless => "phone_checked"
check_phone
phone_validation if phone_changed?
end
def phone_validation
code = Array.new(8){rand(36).to_s(36)}.join
self.phone_verification_code = code
self.phone_checked = true # update flag
self.save
end
OR
use with caution: skip callbacks
def phone_validation
code = Array.new(8){rand(36).to_s(36)}.join
self.update_column(:phone_verification_code => code)
end
after_update callback causing never ending loop in a model
I resolved the issue by removing the after_update
from model and making a call from controller's function itself.
def change_img
@user = current_user
@user.update_attributes(params[:user])
if(!@user.crop_x.blank? && !@user.crop_y.blank? &&
!@user.crop_w.blank? && !@user.crop_h.blank?)
@user.avatar.reprocess!
end
flash[:notice] = "Successfully updated Image."
render :action => 'crop'
end
Thanks!
How can I avoid running ActiveRecord callbacks?
This solution is Rails 2 only.
I just investigated this and I think I have a solution. There are two ActiveRecord private methods that you can use:
update_without_callbacks
create_without_callbacks
You're going to have to use send to call these methods. examples:
p = Person.new(:name => 'foo')
p.send(:create_without_callbacks)
p = Person.find(1)
p.send(:update_without_callbacks)
This is definitely something that you'll only really want to use in the console or while doing some random tests. Hope this helps!
Ruby on Rails SystemStackError stack level too deep validate Wicked gem
You are using validations wrong way. They need to be invoked on class level.
You need to use conditional validations instead:
validates_presence_of :vehicle_location, if: :created_by_admin?
validates_presence_of :pickup, :dropoff, :departure_date, :departure_time, :passengers, if: :created_by_admin?
validates_presence_of :vehicle_location, unless: :created_by_admin?, if: :active_or_parking?
validate :parking_agreement_if_location, unless: :created_by_admin?, if: :active_or_parking?
overriding a value to true and setting the rest to false in rails
In a transaction just set all to false and then update the record you want to true (featured). This is similar to how sometimes its easier to delete records then find them and update them. That is, its easier to just do something like:
tranasction do
SomeModel.delete_all
items.each { |i| SomeModel.create(i) }
end
Which can be easier then looping over SomeModel doing a find() and then updating certain properties.
In your case you just want to mark ALL records a NOT-featured and then just update the one you want as featured, rather than toggling records by hand.
Try something like this:
after_create :set_as_featured
def set_as_featured
self.class.transaction do
self.connection.execute("UPDATE foo SET featured=0")
self.update_attribute(:featured, true)
end
end
Related Topics
Ruby on Rails Group_By (How to Group Events by Month)
Consolidating Duplicate Array Items
Browser-Based Uploads Using Post
Difference Between "<%=" and "<%" When Mixing Ruby with HTML
Setting a Text Field That Has a Jquery Mask on It
Converting Colors (Not Images) with Imagemagick
Mongomapper Association Skips Duplicates
Iterating Over the Registers of a Yardoc '@Macro'
How to Create Nested Model from Partial Rails 6
Why Doesn't This Code Produce the Desired Result
Reply to Thread Google-Api-Ruby-Client
Ruby: Unexpected End-Of-Input, Expecting Keyword_End for If Statement