ruby on rails - Is there a gotcha in the behavior of GlobalID based ActiveRecord objects? -


it's entirely possible doing wrong but!

here's story. have standard, beginners'-level, ror setup (4.2) activejob jobs.

one of jobs calls method on record that's been passed in intended modify record. pretty normal far. looks this:

class genericscraperjob < activejob::base   def perform(record)     # perform job record data     record.update_myself!   end  end 

and update_myself! method looks this:

class recordindb < activerecord::base    def update_myself!        thisis_a_column = newvalue        self.save!     end  end 

now here's mystery - when run tests, , check fixture_instance.thisis_a_column newvalue, test passes. when start app, , see happens in database, not updated expected.

but if self.thisis_a_column = newvalue, works correctly everywhere.

i sure reloaded fixture... did this: fixture_instance = recordindb.find(fixture_inst.id)

it if binding inside activerecord instance changes, depending on when instantiated. inside tests, looks symbols being treated possible messages send self in job, being treated local names. why?

i doubt update_myself! have written works in context. sure failing job , not elsewhere? suspect there problem test giving false positive.

you encountering common gotcha in ruby itself; not specific activejob or rails.

basically, when ruby sees this:

thisis_a_column = newvalue 

it assumes declaring new, locally-scoped variable named thisis_a_column.

if want assign value column managed activerecord, instance-level method, must explicitly use self, this:

self.thisis_a_column = newvalue 

see also: https://stackoverflow.com/a/17991720/4625365


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -