Reputation: 1901
I have the following in the model:
before_save :set_defaults
def set_defaults
self.num_results ||= 5
end
And I create the object in my controller like this:
Search.create!( :keyword => params[:keyword],
:ip_address => request.remote_ip,
:referring_page => request.referer )
Even though I haven't set a value for num_results
it's still getting saved as 0
(which is the default value in the DB schema). The callback function doesn't get called at all. Any clues?
Update:
I turns out the callback does get called, the problem is in:
self.num_results ||= 5
How would I set the default value in ruby? As this doesn't seem to work.
Upvotes: 1
Views: 752
Reputation: 8841
num_results is only set to 5, if num_result is nil. In your case it is set to 0 by default, so the assignemt does not do anything. Try to change your assignment to:
self.num_results = 5
Upvotes: 1