Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.



No description

Darwin Palonpon

on 1 February 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of meta8

Dynamic Attributes Dynamic Finders Dynamic Finders are also Ghost Methods, however this is different from the one that handles dynamic attributes

Dynamic finders are class methods we call on model classes like Task
So we use the class's method_missing(), not the instance's method_missing().

Similar to Dynamic Attributes, method_missing() ensures that your call is legitimate, then it defines a real, non-ghost version of the method by evaluating a String of Code, and calls it with a Dynamic Dispatch. Metaprogramming-Ruby Chapter 8: Inside ActiveRecord require 'activerecord'
ActiveRecord::Base.establish_connection :adapter => "sqlite3",
:database => "dbfile"
ActiveRecord::Base.connection.create_table :tasks do |t|
t.string :description
t.boolean :completed

class Task < ActiveRecord::Base; end task = Task.new
task.description = 'Clean up garage'
task.completed = true
task.description # => "Clean up garage"
task.completed? # => true # Find all completed tasks

# Find the first completed task where description == 'Clean up garage'
Task.find_by_description_and_completed('Clean up garage', true)

# Find the first task where description == 'Water plants',
# or create it if it doesn't exist
Task.find_or_create_by_description('Water plants')

# Find the first task where description == 'Get some sleep',
# and raise an exception if it doesn't exist
Task.find_by_description!('Get some sleep') When you access a database-backed attribute for the first time.. That attribute is a Ghost Method.

ActiveRecord::Base#method_missing() takes this opportunity to turn this Ghost Method into a real method.

It also dynamically defines read, write, and question accessors for all the other database columns.

So the next time you call that attribute(or any other database-backed attribute), you will find a real accessor method, and you don't have to enter method_missing anymore. When you access attributes that are not backed by a database column, like calculated fields... Looking at the code below:

my_query = "tasks.*, (description like '%garage%') as heavy_job"
task = Task.find(:first, :select => my_query)
task.heavy_job? # => true

Attributes like "heavy_job" can be different for each object, so there's no point in generating Dynamic Methods to access them.

When you first created your ActiveRecord object,
ActiveRecord::Base#initialize compiled a list of the column names, so it knows that when you are calling one of these names, you are reading an attribute, so it retrieves the value for you. For example, if you call find_all_by_description_and_completed(), the generated String of Code would look like this:

def self.find_all_by_description_and_completed(*args)
options = args.extract_options!
attributes = construct_attributes_from_arguments(
finder_options = { :conditions => attributes }

if options[:conditions]
with_scope(:find => finder_options) do
find(:all, options)
find(:all, options.merge(finder_options))
Full transcript