ROR + find_or_initialize_by_id Method Functionality?
In Model, I am using 开发者_开发技巧"find_or_initialize"
to save an record. I assume that this function will search the record in the database, if it found then no action and if did not find then save the record. Am I correct or not ?
But, confusion : If the record is already exists, and one of the field get updated then will this function update the record or not ?
Is there any other method to resolve it ?
Please suggest me to resolve this issue... Thanks
find_or_initialize_by_...
won't ever save the record it will only instantiate a new object with the provided parameters set.
If you want the record to be saved automatically (if it's not found) then you should be using find_or_create_by_...
instead.
In either case, any changes you make to the object will have to be followed by a save
if you want them saved to the database. Neither of these methods will save anything you do after it's called.
精彩评论