Prevent attributes vs local variable conflict in activerecord models

Hello, I need a suggestion about a possible best practice.
Consider the following piece of code

class ThumbnailImage < ActiveRecord::Base

more code

public
#
#
#
def request_thumbnail(args = {})
  width   = args[:width]  ||= nil
  height  = args[:height] ||= nil

  # do something
  # with local variables width and height
  # and get job object

  # update image details
  self.width            = job.width
  self.height           = job.height
  self.requested_at     = job.submission_time
  self.webthumb_id      = job.id

  return append_to_fetch_queue()
end

end

As you can see in the same method I have both a local variable called
width and an activerecord table column called width.
To prevent weird behaviors I used self to identify local object
accessor method but I don’t know if this is the best practice to
prevent local variables to conflict with active record attributes.

How do you usually handle this case?

Simone C. wrote:

As you can see in the same method I have both a local variable called
width and an activerecord table column called width.
To prevent weird behaviors I used self to identify local object
accessor method but I don’t know if this is the best practice to
prevent local variables to conflict with active record attributes.

How do you usually handle this case?

AFAIK, you can’t assign to attributes without self at all, right because
Ruby considers such assignments explicitely as creation or assignment of
local variables.

mortee

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs