Created
July 19, 2011 18:05
-
-
Save emmanuel/1093291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "datamapper" | |
DataMapper.setup(:default, "sqlite::memory:") | |
DataMapper::Logger.new(STDOUT, :debug) | |
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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment