# Prevent attributes vs local variable conflict in activerecord models

**URL:** <https://www.ruby-forum.com/t/prevent-attributes-vs-local-variable-conflict-in-activerecord-models/117417>\
**Category:** Ruby\
**Created:** [October 16, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/prevent-attributes-vs-local-variable-conflict-in-activerecord-models/117417 "2007-10-16T18:35:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![overclokkato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c67d28/32.png) [@overclokkato](https://www.ruby-forum.com/u/overclokkato)\
**Post date:** [October 16, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/prevent-attributes-vs-local-variable-conflict-in-activerecord-models/117417/1 "2007-10-16T18:35:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![overclokkato](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c67d28/32.png) [@overclokkato](https://www.ruby-forum.com/u/overclokkato)\
**Post date:** [October 16, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/prevent-attributes-vs-local-variable-conflict-in-activerecord-models/117417/2 "2007-10-16T20:17:54Z")

</div>

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
