<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - Convert MySQl query into Rails]]></title>
		<link>http://railsforum.com/viewtopic.php?id=51812</link>
		<description><![CDATA[The most recent posts in Convert MySQl query into Rails.]]></description>
		<lastBuildDate>Tue, 08 Jan 2013 14:33:13 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=158148#p158148</link>
			<description><![CDATA[<p>I was able to get it to work. It is however printing out some weird data. </p><p>Here is what I did to get it to work. <br /></p><div class="codebox"><pre><code>Statsdeath.select(&quot;player SUM(amount)&quot;).group(:player).order(&quot;SUM(amount) DESC&quot;).limit(1).sum(:amount)</code></pre></div><p>It is however returning a Hash that is hard to work with. Instead of the keys being player and amount, they are the player&#039;s name and the amount. </p><div class="codebox"><pre><code>{&quot;foo&quot;=&gt;73}</code></pre></div><p>Is this the best way to associate a player with their amount of deaths?</p>]]></description>
			<author><![CDATA[dummy@example.com (Wnt2bsleepin)]]></author>
			<pubDate>Tue, 08 Jan 2013 14:33:13 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=158148#p158148</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=158053#p158053</link>
			<description><![CDATA[<p>I put it both in the controller and the view, both times it gave me the same error. </p><p>Controller<br /></p><div class="codebox"><pre><code>@mostKilledplayer = Statsdeath.select(&quot;player SUM(amount)&quot;).group(&#039;player&#039;).order(&quot;SUM(amount) DESC&quot;).first

Error: 
Mysql2::Error: 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 &#039;SUM(amount) FROM `Stats_death`  GROUP BY player ORDER BY SUM(amount) DESC LIMIT &#039; at line 1: SELECT  player SUM(amount) FROM `Stats_death`  GROUP BY player ORDER BY SUM(amount) DESC LIMIT 1</code></pre></div><p>View<br /></p><div class="codebox"><pre><code>Most Killed Player: &lt;%=@mostKilledplayer.first%&gt;

Error: Mysql2::Error: 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 &#039;SUM(amount) FROM `Stats_death`  GROUP BY player ORDER BY SUM(amount) DESC LIMIT &#039; at line 1: SELECT  player SUM(amount) FROM `Stats_death`  GROUP BY player ORDER BY SUM(amount) DESC LIMIT 1</code></pre></div><p>I didn&#039;t have them like that at the same time. I first changed the controller, then when that didn&#039;t work I put it in the view. They both produce the same error. It is occurring on the line that I changed, so it&#039;s nothing wrong with the other code.</p>]]></description>
			<author><![CDATA[dummy@example.com (Wnt2bsleepin)]]></author>
			<pubDate>Fri, 04 Jan 2013 14:40:22 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=158053#p158053</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=158016#p158016</link>
			<description><![CDATA[<p>ActiveRecord::Relation (AKA ARel) is the desired result, it&#039;s all about lazy loading which enhances the ability seamlessly to do things like fragment caching, tagging an enumerable, such as .first, on to the end of an ARel result should then cause the actual query to be run.<br />The idea is that ruby is preparing the query to be run before it is actually needed. Using the ARel should then cause the SQL to be run.</p><p>wherever possible It is desirable to use ruby methods rather than direct SQL statements as Ruby has the arrogance (rightly so) to believe it will do a better job of :-</p><p>1) Optimising the query to be run as efficiently as possible.</p><p>2) Making sure that the SQL that is actually run is as close as possible to the correct syntax for the database used according the the adapter specified in the database.yml for the current environent (Development, Test, Production)</p><p>The idea is that this enables the developer to concentrate more on getting the desired results and less on worrying about the intended database that is going to be used.<br />It doesn&#039;t always work that way, but 99.9% of the time ruby is very very good at achieving both those objectives.</p><p>So given my solution that returns an ActiveRecord::Relation do you get the desired result when you tag on a .first? If not what is the error?</p>]]></description>
			<author><![CDATA[dummy@example.com (jamesw)]]></author>
			<pubDate>Thu, 03 Jan 2013 19:22:11 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=158016#p158016</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=158011#p158011</link>
			<description><![CDATA[<p>I actually posted the wrong SQL. I am converting this from PHP, so I got them confused. This returns the correct data set on my test db.</p><div class="codebox"><pre><code>SELECT player,SUM(amount) FROM Stats_death GROUP BY player ORDER BY SUM(amount) desc LIMIT 1</code></pre></div><p>I was able to get what you posted to work, but it is returning an ActiveRecord::Relation. It was before returning the classname followed by the data. I used to get the following back</p><div class="codebox"><pre><code>#&lt;Statskill player: &quot;specialrose&quot;&gt;]</code></pre></div><p>now I am getting this<br /></p><div class="codebox"><pre><code>ActiveRecord::Relation:0x007fd5be189200&amp;gt</code></pre></div><p>Here is how I modified your code to work. </p><div class="codebox"><pre><code>Statsdeath.select(&quot;player SUM(amount)&quot;).group(&#039;player&#039;).order(&quot;SUM(amount) DESC&quot;)</code></pre></div><p>Putting a .first on the end results in an error, and using .limit(1) returns the relation, so nothing changed.</p>]]></description>
			<author><![CDATA[dummy@example.com (Wnt2bsleepin)]]></author>
			<pubDate>Thu, 03 Jan 2013 16:48:48 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=158011#p158011</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157988#p157988</link>
			<description><![CDATA[<p>Now things are clearer.<br />The SQL you have doesn&#039;t really work, it just looks like it works as it doesn&#039;t take care of a player appearing more than once and you are wanting to find the player that has died the most amount of times regardless of any other factors.</p><p>Take the following data set as an example</p><div class="codebox"><pre><code>1  specialrose  Skeleton  3  1
2  specialrose  Creeper  5  1
3  b                     Skeleton  3  1
4  b                     Creeper  2  1
5  b                     Bee  2  1
6  b                     Dragon  2  1</code></pre></div><p>In the above example specialrose has died 8 times an b has died 9 times. Your SQL would pull out special rose as being the person that has died the most which is clearly not the case (I think!!!! <img src="http://railsforum.com/img/smilies/smile.png" width="15" height="15" alt="smile" />)</p><p>you actually need a bit of complex logic involving grouping and summing to get the data you need which could be quite computationally expensive. <br />Possibly this might work for you<br /></p><div class="codebox"><pre><code>StatsDeath.group([&#039;player&#039;, &#039;amount&#039;]).order(&#039;sum(amount DESC&#039;))</code></pre></div><p>I think (untested) the above should work for you and you should just be able to tag on a .first if you just want the players name.</p><p>Make that into a method on the StatsDeath model (def most_died_player or something like) which will make it simpler to test and easier to refactor if you need.</p><p>I URGE you to write a unit test for this using similar data to the above.</p>]]></description>
			<author><![CDATA[dummy@example.com (jamesw)]]></author>
			<pubDate>Thu, 03 Jan 2013 01:10:05 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157988#p157988</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157984#p157984</link>
			<description><![CDATA[<p>I see what you are saying, and most of the tables I have worked with are like this. The table I am trying to pull data from has the following columns. </p><p>counter(int)|player(varchar)|cause(varchar)|amount(int)|entity(int). </p><p>The cause of death is the name of the mob, the entity is a boolean variable, 1 or 0, to signify that the cause of death was by a mob. I am trying to find the player who has died the most amount of times. It is as you said, and I am sorry for not including this, a person can be listed more than once. It seems they are listed once for each type of entity. For example</p><p>The top 2 rows of the db<br /></p><div class="codebox"><pre><code>1  specialrose  Skeleton  3  1
2  specialrose  Creeper  5  1</code></pre></div><p>The code I listed works when I execute it in PHPMyAdmin, but it does not return the proper result set when I execute it in rails using Model.find_by_sql</p>]]></description>
			<author><![CDATA[dummy@example.com (Wnt2bsleepin)]]></author>
			<pubDate>Wed, 02 Jan 2013 15:45:59 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157984#p157984</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157975#p157975</link>
			<description><![CDATA[<p>O.K., The fact that you have an amount column that you are both summing and ordering by is somewhat confusing. It implies that there would be more than one stats_death record for a player and that each record may contain an amount &gt; 1</p><p>Given the above your SQL statement is ambiguous as to it&#039;s intention is possibly an indication of why you, and I am struggling to get you to the right solution.</p><p>Think about it, and see if you can put into English the actual result you are looking for rather than trying to figure out an ambiguous SQL statement conversion.</p><p>Order the data first, or use the group_by function, whichever best suits your needs.</p><p>Normally in a relational database you would have a players table linked to a stats_death table in a one to many relationship. If you had this then the approach would be slightly different, you would find the correct player record then sum the amount column for all the stats_death records fro that player which would look something like this<br />Player.find(some_id).stat_deaths.sum(:amount)</p>]]></description>
			<author><![CDATA[dummy@example.com (jamesw)]]></author>
			<pubDate>Wed, 02 Jan 2013 08:36:44 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157975#p157975</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157968#p157968</link>
			<description><![CDATA[<p>This is being pulled from one table. They also are not generated by rails, so I specified the table names. Does it matter in which order I chain them?</p>]]></description>
			<author><![CDATA[dummy@example.com (Wnt2bsleepin)]]></author>
			<pubDate>Tue, 01 Jan 2013 23:42:42 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157968#p157968</guid>
		</item>
		<item>
			<title><![CDATA[Re: Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157961#p157961</link>
			<description><![CDATA[<p>There&#039;s not enough information to answer your question.<br />What tables do you have and what are there relationships? How have you represented those relationships in your models?<br />You would typically write these as two separate scopes and chain them together when you need them using .order, .sum and the various finders.</p>]]></description>
			<author><![CDATA[dummy@example.com (jamesw)]]></author>
			<pubDate>Tue, 01 Jan 2013 09:05:35 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157961#p157961</guid>
		</item>
		<item>
			<title><![CDATA[Convert MySQl query into Rails]]></title>
			<link>http://railsforum.com/viewtopic.php?pid=157951#p157951</link>
			<description><![CDATA[<p>Hello, I am trying to convert a MySQL query into a rails query. Right now, it only returns the player&#039;s name, but I also need an integer value for how many times he died. </p><div class="codebox"><pre><code>SELECT player,SUM(amount) FROM Stats_death ORDER BY amount desc LIMIT 1</code></pre></div><p>Any help is appreciated.</p>]]></description>
			<author><![CDATA[dummy@example.com (Wnt2bsleepin)]]></author>
			<pubDate>Tue, 01 Jan 2013 02:42:10 +0000</pubDate>
			<guid>http://railsforum.com/viewtopic.php?pid=157951#p157951</guid>
		</item>
	</channel>
</rss>
