Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
module FromParam
extend ActiveSupport::Concern
module ClassMethods
def [](key)
find_by_slug(key) || find_by_id(key)
end
def from_param(param)
self[param]
end
def from_param!(param)
from_param(param) or raise MongoMapper::DocumentNotFound
end
end
end
MongoMapper::Document.plugin(FromParam)
class Post
include MongoMapper::Document
key :slug
many :comments
end
class Comment
include MongoMapper::Document
key :slug
belongs_to :post
end
post = Post.create(:slug => 'post')
com1 = Comment.create(:slug => 'comment1', :post => post)
com2 = Comment.create(:slug => 'comment1', :post => post)
# This raises an ArgumentError as expected b/c Comment does respond_to :from_param
Post.first.comments.from_param
# However, because from_param doesn't return a Plucky::Query, it continues up the
# method missing chain.
Post.first.comments.from_param('comment1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.