Skip to content

Instantly share code, notes, and snippets.

@twobitfool
Created July 19, 2011 16:08
Show Gist options
  • Select an option

  • Save twobitfool/1092916 to your computer and use it in GitHub Desktop.

Select an option

Save twobitfool/1092916 to your computer and use it in GitHub Desktop.

Revisions

  1. twobitfool created this gist Jul 19, 2011.
    25 changes: 25 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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