def collection(name, model, reference = to_reference)
      reader = :"#{name}_ids"

      attribute reader, accessors: :reader

      define_method(:"#{name}_ids=") do |value|
        @_memo.delete(name)
        @attributes[reader] = value
      end

      define_method(name) do
        return [] if self.id.nil?
        @_memo[name] ||= begin
                           model = Ork::Utils.const(self.class, model)
                           model.all(@attributes[reader].to_a)
                         end
      end

      define_method(:"#{name}_add") do |object|
        assert_valid_class object, model

        @attributes[reader] = Array(@attributes[reader]) << object.id
        @_memo[name] << object if @_memo[name]
      end

      define_method(:"#{name}_remove") do |object|
        assert_valid_class object, model

        @_memo[name].delete(object) if @_memo[name]
        @attributes[reader].delete(object.id) and object if @attributes[reader]
      end
    end
def collection(name, model, reference = to_reference)
      reader = :"#{name}_ids"

      attribute reader, accessors: :reader

      define_method(:"#{name}_ids=") do |value|
        @_memo.delete(name)
        @attributes[reader] = value
      end

      define_method(name) do
        return [] if self.id.nil?
        @_memo[name] ||= begin
                           model = Ork::Utils.const(self.class, model)
                           model.all(@attributes[reader].to_a)
                         end
      end

      define_method(:"#{name}_add") do |object|
        assert_valid_class object, model

        @attributes[reader] = Array(@attributes[reader]) << object.id
        @_memo[name] << object if @_memo[name]
      end

      define_method(:"#{name}_remove") do |object|
        assert_valid_class object, model

        @_memo[name].delete(object) if @_memo[name]
        @attributes[reader].delete(object.id) and object if @attributes[reader]
      end
    end
A macro for defining a method which basically does a find.

 Example:
   class Post
     include Ork::Document

     reference :user, :User
   end

   class User
     include Ork::Document

     collection :posts, :Post
   end

   # is the same as

   class User
     include Ork::Document

     attribute posts_ids

     def posts
       Post.all(self.posts_ids)
     end

     def posts_add(post)
       self.posts_ids << post.id
     end
   end