ruby on rails - Can you add a find_or_create to the before_save call back on ActiveRecord? -
i have model see if object exists before saving. , if does, not create new object, use existing one.
is possible add directly before_save activerecord class of model ?
i believe impossible because can not change value of self.
activerecord provides functionality. can like:
user.find_or_initialize_by_email('some@email.com') this return user database, if found, or new user email set passed parameter. can use object otherwise , call save when done (which update or create needed).
more info on http://api.rubyonrails.org/classes/activerecord/base.html (search page find_or_initialize_by_)
Comments
Post a Comment