Skip to content

Instantly share code, notes, and snippets.

@kaichen
Created October 10, 2009 02:11
Show Gist options
  • Save kaichen/206527 to your computer and use it in GitHub Desktop.
Save kaichen/206527 to your computer and use it in GitHub Desktop.
#!/opt/local/bin/ruby
require 'rubygems'
require 'dm-core'
require 'dm-sweatshop'
require 'dm-aggregates'
require 'pp'
include DataMapper::Sweatshop::Unique
DataMapper.logger = DataMapper::Logger.new($stderr, :debug)
DataMapper.setup(:default, 'mysql://localhost/for_testing')
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :user_id, Integer
belongs_to :user
end
class User
include DataMapper::Resource
property :id, Serial
property :login, String
property :role, String
has n, :posts
end
DataMapper.auto_migrate!
User.fixture {{
:login => unique { /\w+/.gen },
:role => "dev"
}}
Post.fix {{
:title => unique { |x| "Cha" * x.to_i },
:user => User.pick
}}
5.times { User.gen }
10.times { Post.gen }
user_ids = User.all(:role => "admin").map {|u| u.id}
# MysqlError: (mysql_errno=1064, sql_state=42000)
# You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1 Query: SELECT COUNT(*) FROM `posts` WHERE (`user_id` IN ())
Post.count :user_id => user_ids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment