Thursday, May 3, 2007

self centered

So I'm working on this Rails project where I am creating a model object with associations all-at-once using a technique recommended by Mr Josh Susser.

The relevant part of my code looks like this:


def recipient_list=(recipient_emails)
puts "Assigning recipient list: #{recipient_emails}"
if new_record?
@recipient_list = recipient_emails
else
create_recipients(recipient_emails)
end
end

def after_create
self.recipient_list = @recipient_list if @recipient_list
end


Originally this wasn't invoking the recipient_list= method during after_create
because I had removed the "self." thinking it was superfluous. Adding it back in
resulted in the desired behavior. But why?

Best I can figure is that the following looks like a local variable assignment to Ruby:


recipient_list = @recipient_list if @recipient_list


Whereas the following is a method invocation:

self.recipient_list = @recipient_list if @recipient_list

No comments: