Skip to content

Instantly share code, notes, and snippets.

@emmanuel
Created July 19, 2011 18:05
Show Gist options
  • Select an option

  • Save emmanuel/1093291 to your computer and use it in GitHub Desktop.

Select an option

Save emmanuel/1093291 to your computer and use it in GitHub Desktop.

Revisions

  1. emmanuel revised this gist Jul 19, 2011. 1 changed file with 35 additions and 4 deletions.
    39 changes: 35 additions & 4 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    require "rubygems"
    require "datamapper"

    DataMapper.setup(:default, "sqlite::memory:")
    DataMapper::Logger.new(STDOUT, :debug)

    class Recipe
    include DataMapper::Resource

    @@ -16,8 +22,8 @@ class Recipe
    property :cooktime, Integer, :default => 0

    belongs_to :user
    has n, :comments
    has n, :favorites
    # has n, :comments
    # has n, :favorites

    end

    @@ -32,14 +38,39 @@ class User
    }

    has n, :recipes
    has n, :comments
    has n, :favorites
    # has n, :comments
    # has n, :favorites

    #other unrelated stuff here.
    end

    DataMapper.auto_migrate!

    #Problem code
    user = User.create(
    :login => 'CannedUser'# ,
    # :password => 'abc456',
    # :password_confirmation => 'abc456'
    )

    useher = User.create(
    :login => 'CannedUseher'# ,
    # :password => 'abc456',
    # :password_confirmation => 'abc456'
    )

    recipe = Recipe.create(
    :name => "Canned Brownies",
    :description => "A description of a recipe.",
    :slug => "Canned_Brownies",
    :user => user
    )

    slug = "Canned Brownies"
    # user = User.create(:login => "my_special_user")
    # Recipe.create(:user => user, :name => "test", :slug => slug, :description => "value")

    recipe = Recipe.first(:slug => slug)
    user = recipe.user
    p user
    #recipe.user = 1 (the id); not an object
  2. @doomspork doomspork created this gist Jul 19, 2011.
    45 changes: 45 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    class Recipe
    include DataMapper::Resource

    property :id, Serial
    property :name, String, :required => true,
    :messages => {
    :presence => 'A recipe requires a name.'
    }
    property :slug, Slug, :required => true, :unique => true,
    :messages => {
    :presence => 'A slug is required!',
    :is_unique => 'That slug is already in use.'
    }
    property :description, Text, :required => true
    property :preptime, Integer, :default => 0
    property :cooktime, Integer, :default => 0

    belongs_to :user
    has n, :comments
    has n, :favorites

    end

    class User
    include DataMapper::Resource

    property :id, Serial
    property :login, String, :required => true, :unique => true, :length => 6..64,
    :messages => {
    :presence => 'A login is required.',
    :is_unique => 'That login is taken!'
    }

    has n, :recipes
    has n, :comments
    has n, :favorites

    #other unrelated stuff here.
    end


    #Problem code
    recipe = Recipe.first(:slug => slug)
    user = recipe.user
    #recipe.user = 1 (the id); not an object