Skip to content

Instantly share code, notes, and snippets.

@MarkMenard
Created January 28, 2010 16:29
Show Gist options
  • Save MarkMenard/288885 to your computer and use it in GitHub Desktop.
Save MarkMenard/288885 to your computer and use it in GitHub Desktop.
class Person < ActiveRecord::Base
# Everything in here are methods on the Person object.
class << self
def find_by_name (name)
# implement it
end
end
# Assume the table has a name and address column.
def name= (name)
# @name is an instance field that AR doesn't know about.
# AR doesn't persist instance variables. It only persists
# fields that are in the database and stored in its attributes
# hash.
@name = name
end
def some_method
name = "Mark" # This defines a method variable named 'name'
this.name = "Mark" # This calls the name= methods on the instance self.
end
def some_other_method
some_condition ? "mark" : 'ryan'
end
def the_other_method
name = some_other_method.capitalize
end
end
p = Person.new
p.name = 'Ryan'
p.address = 'New York'
p.save # This does not write Ryan into the name field, it does write 'New York' into the address column.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment