Hi,

I have two model, challenge(embeds_many :tasks) and task(embedded_in
:challenge),
finally database structue is like this

{ "_id" : ObjectId("4db8b0524f0b495c3a7dbba4"), "title" : "Testing1",
"created" : ISODate("2011-12-12T00:00:00Z"),
"tasks" : [
{
"name" : "task no 1",
"score" : "5"
},
{
"name" : "task no2",
"score" : "6"
}
] }

{ "_id" : ObjectId("4db8b0524f0b495c3a7dbba5"), "title" : "Testing2",
"created" : ISODate("2011-12-12T00:00:00Z"),
"tasks" : [
{
"name" : "task no 1",
"score" : "7"
},
{
"name" : "task no2",
"score" : "8"
}
] }

Here i have to select top 5 challenges having highest task score. I am
using mongodb as database and i come to know
there is concept of "Map/reduce grouping" but not able to figure it out.

i was writting this code in my controller but it become more complicated.

def whowins
@challenge = Challenge.where(:_id => "4ef1a6a454b53001a4000067").first
$wins ={"first"=>{"id"=>"1", "score"=>"1"},"second"=>{"id"=>"2",
"score"=>"2"},"third"=>{"id"=>"3", "score"=>"3"},"fourth"=>{"id"=>"4",
"score"=>"4"},"fifth"=>{"id"=>"5", "score"=>"5"}}
temp ={"id"=>"10","score"=>"20"}
aTotalScore = 0

if @challenge.instance_of?Challenge

@challenge.tasks.each_with_index do |orgTasks,index|
aTotalScore += orgTasks.score.to_i
end
$wins["first"]["id"]= @challenge.user_id
$wins["first"]["score"]= aTotalScore
aTotalScore = 0

@challenge.child_challenges.each do |aChildChallenge|
aChildChallenge.tasks.each_with_index do |eachTasks,index|
aTotalScore += eachTasks.score.to_i
end
#raise aTotalScore.inspect
if aTotalScore > $wins["first"]["score"]
#raise "first less"
whoWinning aTotalScore, $wins["first"]["score"],
aChildChallenge.user_id, 1
aTotalScore = 0
next if aTotalScore = 0
elsif aTotalScore > $wins["second"]["score"]
#raise "decond less"
whoWinning aTotalScore, $wins["second"]["score"],
aChildChallenge.user_id, 2
aTotalScore = 0
next if aTotalScore = 0
elsif aTotalScore > $wins["third"]["score"]
#raise "third less"
whoWinning aTotalScore, $wins["third"]["score"],
aChildChallenge.user_id, 3
aTotalScore = 0
next if aTotalScore = 0
elsif aTotalScore > $wins["fourth"]["score"]
#raise "fourht less"
whoWinning aTotalScore, $wins["fourth"]["score"],
aChildChallenge.user_id, 4
aTotalScore = 0
next if aTotalScore = 0
else
#raise "fifth less"
whoWinning aTotalScore, $wins["fifth"]["score"],
aChildChallenge.user_id, 5
aTotalScore = 0
next if aTotalScore = 0
end
end

else
raise "childchallenge"
end
end

private

def whoWinning(tempScore, winnerScore, winnerId, aPossition )
aTScore = 0
aTId = 0
if aPossition == 5
$wins["fifth"]["score"],$wins["fifth"]["id"] = tempScore,winnerId
raise $wins.inspect
return
else
if aPossition == 1
aTScore,$wins["first"]["score"] = $wins["first"]["score"],tempScore
aTId = $wins["second"]["id"]
$wins["second"]["score"],$wins["first"]["id"] = aTScore,winnerId
if $wins["third"]["score"].to_i > $wins["second"]["score"]
whoWinning $wins["third"]["score"],
$wins["second"]["score"], aTId, 2
else
return
end
end

if aPossition == 2
aTScore,$wins["second"]["score"] =
$wins["second"]["score"],tempScore
aTId = $wins["third"]["id"]
$wins["third"]["score"],$wins["second"]["id"] = aTScore,winnerId
if $wins["fourth"]["score"] > $wins["third"]["score"]
whoWinning $wins["fourth"]["score"],
$wins["third"]["score"], aTId, 3
else
return
end
end

if aPossition == 3
aTScore,$wins["third"]["score"] = $wins["third"]["score"],tempScore
aTId = $wins["fourth"]["id"]
$wins["fourth"]["score"],$wins["third"]["id"] = aTScore,winnerId
if $wins["fifth"]["score"] > $wins["fourth"]["score"]
whoWinning $wins["fifth"]["score"],
$wins["fourth"]["score"], aTId, 4
else
return
end
end

if aPossition == 4
aTScore,$wins["fourth"]["score"] =
$wins["fourth"]["score"],tempScore
aTId = $wins["fifth"]["id"]
$wins["fifth"]["score"],$wins["fourth"]["id"] = aTScore,winnerId
#whoWinning $wins["fifth"]["score"],
$wins["fourth"]["score"], aTId, 5
end
end
end

I m new to mongodb, any help or suggetion will be highly apreciated.
Tks in advance

+++++++++++++++++++++++
Pravin Mishra

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 22, '11 at 7:45p
activeDec 22, '11 at 7:45p
posts1
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Pravin Mishra: 1 post

People

Translate

site design / logo © 2021 Grokbase