Skip to content

Instantly share code, notes, and snippets.

@twobitfool
Created July 19, 2011 16:08
Show Gist options
  • Save twobitfool/1092916 to your computer and use it in GitHub Desktop.
Save twobitfool/1092916 to your computer and use it in GitHub Desktop.
class Post
include DataMapper::Resource
property :id, Serial
belongs_to :author, :default => lambda { |r, a| Author.first }
end
class Author
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :posts
end
bill = Author.create(:name => "Bill")
dave = Author.create(:name => "Dave")
post = Post.create(:author_id => dave.id)
puts post.author.name
=> "Bill"
# default value of author (on post) is beating out the author_id passed in to create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment