Questions tagged [before-save]

An ActiveRecord callback that is executed after validating an object but before writing it to the database.

before_save is one of the standard ActiveRecord callbacks. There are three sets of callbacks:

  • Creating an Object (in order of execution):
    1. before_validation
    2. after_validation
    3. before_save
    4. around_save
    5. before_create
    6. around_create
    7. after_create
    8. after_save
  • Updating an Object (in order of execution):
    1. before_validation
    2. after_validation
    3. before_save
    4. around_save
    5. before_update
    6. around_update
    7. after_update
    8. after_save
  • Destroying an Object (in order of execution):
    1. before_destroy
    2. around_destroy
    3. after_destroy
154 questions
24
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…
17
votes
3 answers

Rails - Get old value in before_save

I'm trying to get the old value in the before_save by adding "_was" to my value but it doesn't seem to work. Here is my code: before_save :get_old_title def get_old_title puts "old value #{self.title_was} => #{self.title}" end Both…
user2037696
  • 945
  • 3
  • 13
  • 31
9
votes
1 answer

Is :on => :create valid for a before_save callback in Rails 3.2.3

As you know, before_save callbacks are executed prior to before_create callbacks. Therefore, some people have suggested that in would be better to use before_save :method, :on => :create instead of before_create so that the callback method is…
Nathan
  • 7,256
  • 8
  • 31
  • 42
8
votes
2 answers

before_save, strip a string

I'm trying to strip the whitespaces of the variable Username in my User Model. I'm using before_save do self.username.strip! end but it doesn't seem to work, am i missing something ?
Mini John
  • 7,577
  • 8
  • 52
  • 103
7
votes
4 answers

Yii on update, detect if a specific AR property has been changed on beforeSave()

I am raising a Yii event on beforeSave of the model, which should only be fired if a specific property of the model is changed. The only way I can think of how to do this at the moment is by creating a new AR object and querying the DB for the old…
Dzhuneyt
  • 7,039
  • 11
  • 56
  • 108
6
votes
1 answer

Check if a model was modified or created on before_save event

I want to check if a model is being created in the before_save callback of Rails. I also want to check if it has been modified (when updating). Thanks
Tony
  • 9,552
  • 18
  • 77
  • 131
6
votes
1 answer

mark_for_destruction in before_save

Whats wrong with this before_save-callback? class Order < ActiveRecord::Base has_many :line_items, :dependent => :destroy, :inverse_of => :order accepts_nested_attributes_for :line_items attr_accessible :line_items_attributes …
NovapaX
  • 147
  • 1
  • 10
4
votes
1 answer

How to know if value changed in Parse Cloud Code afterSave hook?

I want to send push notifications every time the value of a single key of my object changes in a parse cloud code afterSave hook. Parse.Cloud.afterSave("Channel", function(request) { var channel = request.object // TODO: check if value of…
4
votes
2 answers

Register multiple before_save conditional callbacks on Rails 4

I have two different calculations to make on my model for two different fields. Before saving. Class Event < ActiveRecord::Base #some relations before_save :method1, unless: :some_field_1? before_save :method2, unless: :some_field_2? def…
limoragni
  • 2,578
  • 1
  • 28
  • 49
4
votes
1 answer

Cakephp 2.1 beforeSave not working

I have a User Model, a UsersController and an AccountController which uses the User Model (the account controller is used when an account is created, login, logout). Everything works fine, except the beforeSave function in AccountController. I'm…
Christian Strang
  • 8,336
  • 4
  • 39
  • 50
3
votes
3 answers

before save event, need help for looping two cells at the same time

I'm trying to do a beforesave event, not allowing users to save if one of two given cells are empty. What I managed to do so far is linking column 13 (M) and cell A4. What I'd like to do is applying the event to a combination of two range and rows,…
thom80
  • 33
  • 5
3
votes
4 answers

before_save if attribute.present?

before_save :date_started_sets_deadline, if date_started.present? I don't want this before_save to run if :date_started == nil. I've tried various versions of the above line so not sure if I have to change that or the method itself. def…
AnthonyGalli.com
  • 2,578
  • 4
  • 22
  • 62
3
votes
2 answers

YII - Why to use beforeSave() when you can code before Save() function

I am aware of functionality of function beforeSave() in YII that this function is used to perform something, which we want to perform before our data saved. However, as far as we want to implement this before our data got save to database, can't we…
CodeWithCoffee
  • 1,850
  • 1
  • 12
  • 33
3
votes
2 answers

Run before_save after changed in attribute in a related model

I am having some troubles today and cannot think outside the box to fix this one. Basically I have a model called Airplane, which has_many Payments. Each payment can be divided in many Installments. Ok! Here is the info: Model Airplane - has_many…
Guido
  • 377
  • 2
  • 13
3
votes
1 answer

Rails: before_save variable contents

I'm reading a guide about Rails and I came across this line in a Model class: before_save { |user| user.email = email.downcase } This is to make sure that the email address is lowercase before it hits the database (but you already know that,…
RileyE
  • 10,308
  • 12
  • 60
  • 103
1
2 3
10 11