ruby on rails - RoR: first_or_initialize block doesn't save -
i have following code, works fine no errors models never saved...
myarray.each |item| r = mymodel.unscoped.where(:site_id => @site.id, :url => item['permalink_url']).first_or_initialize |r| r.title = 'asdasdadaddjfgnfd' r.save! end end
terminal shows sql select statements when attempting find models, update/insert statements never run.
what missing here?
rails first_or_*
methods invoke passed block initialize
or create
part. if record found, methods return passed block never run. check source
so can use block in first_or_*
methods initialize new items, not update existing ones. likely, records such conditions exist , don't updated.
try move update code, like
myarray.each |item| r = mymodel.unscoped.where(:site_id => @site.id, :url => item['permalink_url']).first_or_initialize r.title = 'asdasdadaddjfgnfd' r.save! end
Comments
Post a Comment