Prevent attributes vs local variable conflict in activerecord models

S

Simone Carletti

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?
 
M

mortee

Simone said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top