By Andrew

2011-03-03 16:39:11 8 Comments

In testing a getter/setter pair in a rails model, I've found a good example of behavior I've always thought was odd and inconsistent.

In this example I'm dealing with class Folder < ActiveRecord::Base.

Folder belongs_to :parent, :class_name => 'Folder'

On the getter method, if I use:

def parent_name


def parent_name

...the result is exactly the same, I get the name of the parent folder. However, in the getter method if I use...

def parent_name=(name)
  parent = self.class.find_by_name(name)

... parent becomes nil, but if I use...

def parent_name=(name)
  self.parent = self.class.find_by_name(name)

...then then it works.

So, my question is, why do you need to declare self.method sometimes and why can you just use a local variable?

It seems the need for / use of self in ActiveRecord is inconsistent, and I'd like to understand this better so I don't feel like I'm always guessing whether I need to declare self or not. When should you / should you not use self in ActiveRecord models?


@pinkmexican 2011-03-03 16:59:29

This is because attributes/associations are actually methods(getters/setters) and not local variables. When you state "parent = value" Ruby assumes you want to assign the value to the local variable parent.

Somewhere up the stack there's a setter method "def parent=" and to call that you must use "self.parent = " to tell ruby that you actually want to call a setter and not just set a local variable.

When it comes to getters Ruby looks to see if there's a local variable first and if can't find it then it tries to find a method with the same name which is why your getter method works without "self".

In other words it's not the fault of Rails, but it's how Ruby works inherently.

Hope that helps.

@Andrew 2011-03-03 17:11:46

Wow, perfect. It works as a getter because ruby doesn't find the local and so looks for a method instead. So, in other words, if I mean self I should use self, rather than leave it off. Wow, this was a very, very very helpful explanation.

@Fábio Batista 2011-03-03 21:05:40

Indeed, thanks for the explanation (and for the well-written question).

@jxpx777 2013-10-07 12:48:22

This answer's explanation about the dynamic nature of ActiveRecord models' methods makes this even clearer. Ruby understands the class hierarchy but not necessarily those methods that are dynamically generated or handled via method_missing.

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How to understand nil vs. empty vs. blank in Rails (and Ruby)

  • 2009-05-19 22:51:36
  • Arrel
  • 303945 View
  • 1117 Score
  • 14 Answer
  • Tags:   ruby-on-rails ruby

26 Answered Questions

6 Answered Questions

[SOLVED] Why is it bad style to `rescue Exception => e` in Ruby?

33 Answered Questions

[SOLVED] How do I get the current absolute URL in Ruby on Rails?

6 Answered Questions

[SOLVED] class << self idiom in Ruby

14 Answered Questions

19 Answered Questions

[SOLVED] Purge or recreate a Ruby on Rails database

1 Answered Questions

[SOLVED] Understanding Rails ActiveRecord "single model" self joins

1 Answered Questions

[SOLVED] method_missing override is not working

Sponsored Content