Thursday, January 28, 2010

Models and Default Values

Couple notes:

script/console:
1. reload! - reload all the Rails stuff
2. Model_name.destroy(id_number) - delete an object from a table

Model Validation:
validates_presence_of :url <-- look at spelling of presenCe!!
validates_uniqueness_of :url
See http://juixe.com/techknow/index.php/2006/07/29/rails-model-validators/
also in Model file is Default Values
before_save :default_values
def default_values
if self.column_name == nil
self.column_name = 'hello'
end
nil
end

before_save will let me execute a function before the object gets saved (new or update). I check to see if a value is designated. If not, use the default value.
Use 'nil' at the end to account for strange behavior with a boolean getting set to false. See http://www.neeraj.name/blog/articles/838-before_save-callback-be-careful-accidentally-do-not-return-false

No comments:

Post a Comment