Created
August 4, 2010 18:00
-
-
Save JonKernPA/508525 to your computer and use it in GitHub Desktop.
This shows A User/Idea/Rating model using MongoMapper
This file contains 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
class Idea | |
include MongoMapper::Document | |
key :brilliance, String, :default => "Dark Matter Sucks!" | |
many :ratings | |
belongs_to :user | |
key :user_id, ObjectId | |
def to_s | |
text = "#{user.name} had this IDEA: #{brilliance}\n\tRATINGS:\n" | |
ratings.each do |r| | |
text += "\t#{r.to_s}" | |
text += " [VAIN?]" if user == r.user | |
text += "\n" | |
end unless ratings.empty? | |
text | |
end | |
end |
This file contains 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
class Rating | |
include MongoMapper::Document | |
key :value, Integer, :default => 5 | |
belongs_to :user | |
key :user_id, ObjectId | |
belongs_to :idea | |
key :idea_id, ObjectId | |
def to_s | |
"#{value} by #{user.name}" | |
end | |
end |
This file contains 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
Jon had this IDEA: Ducks walk like ducks. Duh | |
RATINGS: | |
2 by Fred | |
6 by Sally | |
Jon had this IDEA: Ducks talk like ducks. Duh | |
RATINGS: | |
3 by Fred | |
1 by Sally | |
1 by Jon [VAIN?] |
This file contains 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 File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment')) | |
require 'rubygems' | |
require 'mongo' | |
connection = Mongo::Connection.new() | |
db = connection.db("mm-#{Rails.env}") | |
print "Connected to #{db.name}\n" | |
db.collection("User").drop # doesn't work????' | |
User.all.each {|r| r.destroy} | |
Idea.all.each {|r| r.destroy} | |
Rating.all.each {|r| r.destroy} | |
jon = User.create(:name => 'Jon') | |
fred = User.create(:name => 'Fred') | |
sally = User.create(:name => 'Sally') | |
jon_idea1 = Idea.create(:brilliance => "Ducks walk like ducks. Duh", :user => jon) | |
jon_idea2 = Idea.create(:brilliance => "Ducks talk like ducks. Duh", :user => jon) | |
rt1 = Rating.create( :idea => jon_idea1, :user => fred, :value => 2) | |
rt2 = Rating.create( :idea => jon_idea1, :user => sally, :value => 6 ) | |
rt1 = Rating.create( :idea => jon_idea2, :user => fred, :value => 3) | |
rt2 = Rating.create( :idea => jon_idea2, :user => sally, :value => 1 ) | |
rt2 = Rating.create( :idea => jon_idea2, :user => jon, :value => 1 ) | |
Idea.all.each { |s| puts "#{s.to_s}\n" } | |
This file contains 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
class User | |
include MongoMapper::Document | |
key :name, String, :default => "Albert" | |
many :ratings | |
many :ideas | |
def to_s | |
name | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i ran the above from a rails project where the only changes needed were:
config.gem "mongo_mapper"
MongoMapper.database = "todo-#{Rails.env}"
Then my "scratchpad.rb" was under the test directory and it executed.