Method: Sohm::Model.collection

Defined in:
lib/sohm.rb

.collection(name, model, reference = to_reference) ⇒ Object

A macro for defining a method which basically does a find.

Example:

class Post < Sohm::Model
  reference :user, :User
end

class User < Sohm::Model
  collection :posts, :Post
end

# is the same as

class User < Sohm::Model
  def posts
    Post.find(:user_id => self.id)
  end
end


716
717
718
719
720
721
# File 'lib/sohm.rb', line 716

def self.collection(name, model, reference = to_reference)
  define_method name do
    model = Utils.const(self.class, model)
    model.find(:"#{reference}_id" => id)
  end
end