<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog@osd.se</title>
	<atom:link href="http://blog.osd.se/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.osd.se</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 19 Oct 2009 12:20:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How should I work with SQL in Rails?</title>
		<link>http://blog.osd.se/2009/10/05/how-should-i-work-with-sql-in-rails/</link>
		<comments>http://blog.osd.se/2009/10/05/how-should-i-work-with-sql-in-rails/#comments</comments>
		<pubDate>Mon, 05 Oct 2009 20:56:13 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.osd.se/?p=28</guid>
		<description><![CDATA[I recently started working with Rails again. I haven&#8217;t gotten very far yet and I&#8217;m still re-learning all the stuff I forgot since last time. Today I wrote this piece of code:
user.transactions.sum('ABS(amount)', :joins =&#62;
"INNER JOIN tags_transactions ON transactions.id = tags_transactions.transaction_id " +
"INNER JOIN tags ON tags_transactions.tag_id = tags.id", :group =&#62; "tags.name")
 It works, but I [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started working with Rails again. I haven&#8217;t gotten very far yet and I&#8217;m still re-learning all the stuff I forgot since last time. Today I wrote this piece of code:<br />
<code>user.transactions.sum('ABS(amount)', :joins =&gt;<br />
"INNER JOIN tags_transactions ON transactions.id = tags_transactions.transaction_id " +<br />
"INNER JOIN tags ON tags_transactions.tag_id = tags.id", :group =&gt; "tags.name")<br />
</code> It works, but I don&#8217;t really like to have dependencies on my table names in my code (even if it&#8217;s in the model). I wonder if there&#8217;s a way to write this in a better way? (There probably is. The problem is to find out <em>how</em>.)</p>
<p>What I&#8217;m trying to do is to group by a field that I have to join in (in two steps). In this case it&#8217;s a tag name. (An account belongs to a user and has many transactions. A transaction has, and belongs to, many tags.) The full SQL of the (hand-written) query is:<br />
<code>SELECT sum(ABS(amount)), tags.name<br />
FROM transactions<br />
INNER JOIN accounts ON transactions.account_id = accounts.id<br />
INNER JOIN tags_transactions tt ON transactions.id = tt.transaction_id<br />
INNER JOIN tags ON tt.tag_id = tags.id<br />
WHERE ((accounts.user_id = 1))<br />
GROUP BY tags.name;<br />
</code></p>
<p><strong>Update 2009-10-09</strong>: The answer was simple. You can write it like this<br />
<code>group = user.transactions.sum('ABS(amount)', :joins => :tags, :group => 'tags.name')</code>and Rails will figure out how to join in &#8216;tags&#8217; (which is specified in the models). Should have tried that first&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2009/10/05/how-should-i-work-with-sql-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Per-user tags with has_many_polymorphs</title>
		<link>http://blog.osd.se/2007/12/04/per-user-tags-with-has_many_polymorphs/</link>
		<comments>http://blog.osd.se/2007/12/04/per-user-tags-with-has_many_polymorphs/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 11:59:46 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/12/04/per-user-tags-with-has_many_polymorphs/</guid>
		<description><![CDATA[I got a question about how I implemented per-user tagging with has_many_polymorphs. He suggested that I wrote a post about it. And why not?
Most posts that I have read about combining users and tagging was about adding the user scope to the tagging, which means that you still have a global set of tags (for [...]]]></description>
			<content:encoded><![CDATA[<p>I got a question about <a href="/2007/08/01/oskar-on-rails/">how I implemented per-user tagging</a> with <a href="http://blog.evanweaver.com/files/doc/fauna/has_many_polymorphs">has_many_polymorphs</a>. He suggested that I wrote a post about it. And why not?</p>
<p>Most posts that I have read about combining users and tagging was about adding the user scope to the tagging, which means that you still have a global set of tags (for example like del.icio.us). I wanted each user to have his, or her, own set of tags &#8211; a tag belongs to a user &#8211; like Gmail.</p>
<p>This is how I did it:</p>
<p>I used the tagging generator that comes with HMP and generate the Tag and Tagging models. From before, I had the User and Transfer (my taggable) models. Each Transfer belongs to a User.</p>
<p>To the Tag model, add &#8220;belongs_to :user&#8221; and &#8220;validates_uniqueness_of :name, :scope =&gt; [:user_id, :name]&#8220;.</p>
<p>Add &#8220;belongs_to :user&#8221; to the Tagging model. The taggings will belong to a user through the tag (and also through the taggable in my case, but that shouldn&#8217;t be a requirement).</p>
<p>You also need to modify the &#8220;_add_tags&#8221; function in the generated file &#8220;lib/tagging_extensions.rb&#8221;. When the Tag is found or created, you need to do &#8220;tag = Tag.find_or_create_by_name_and_user_id(tag_name, <a href="http://user.id/" target="_blank">user.id</a>)&#8221;, instead of &#8220;tag = Tag.find_or_create_by_name(tag_name)&#8221;, which I believe it was originally. Otherwise it doesn&#8217;t set the user_id in the tag.</p>
<p>Of course you have to update your migrations for the Tag model. You need to add a user_id column to it.</p>
<p>That should be it, but I might have forgotten something.</p>
<p>(Thanks to Alexander Hoth for the idea to this post.)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/12/04/per-user-tags-with-has_many_polymorphs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prototype is a prototype?</title>
		<link>http://blog.osd.se/2007/08/13/prototype-is-a-prototype/</link>
		<comments>http://blog.osd.se/2007/08/13/prototype-is-a-prototype/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 21:46:44 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/08/13/22/</guid>
		<description><![CDATA[In my quest to figure out why Prototype&#8217;s request method sent all my submit buttons, I stumbled upon this, not so pretty, piece of code:
if (value != null &#38;&#38; (element.type != 'submit' &#124;&#124; (!submitted &#38;&#38;
    submit !== false &#38;&#38; (!submit &#124;&#124; key == submit) &#38;&#38; (submitted = true)))) {
It&#8217;s from a changeset [...]]]></description>
			<content:encoded><![CDATA[<p>In my quest to figure out why <a href="http://www.prototypejs.org/">Prototype&#8217;s</a> request method sent all my submit buttons, I stumbled upon this, not so pretty, piece of code:</p>
<pre>if (value != null &amp;&amp; (element.type != 'submit' || (!submitted &amp;&amp;
    submit !== false &amp;&amp; (!submit || key == submit) &amp;&amp; (submitted = true)))) {</pre>
<p>It&#8217;s from <a href="http://dev.rubyonrails.org/changeset/7140">a changeset</a> in Prototype&#8217;s Trac page and is supposed to fix <a href="http://dev.rubyonrails.org/ticket/5031">this bug</a>.</p>
<p>The reason I started reading the code was that I&#8217;m using multiple submit tags with different names. Instead of just varying the value, I set the type tag to &#8220;submit&#8221;. Example:</p>
<pre>&lt;input name="create" value="Create New..." type="submit" /&gt;</pre>
<p>and in Rails I check if <code>params[:create]</code> is set to see if that button was pressed or not. Perhaps this is really bad for some reason that I have missed, I don&#8217;t know (it does work in my version of IE6 aswell), but <a href="http://dev.rubyonrails.org/changeset/7140">the fix</a> doesn&#8217;t solve my problem. It just solves when using multiple input tags with the name submit and vary the name. The reason I don&#8217;t like looking what the value attribute is set to, is that it&#8217;s part of the design and should be free to change, without having to modify the controller. Of course, hidden values are always an alternative, but requiring Javascript to use a simple form seems wrong.</p>
<p>Anyway, I opened up my prototype.js and started reading the code and found the above piece of code. Note the assignment in the end of the second row. I think it&#8217;s intentional.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/08/13/prototype-is-a-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Helium</title>
		<link>http://blog.osd.se/2007/08/12/helium/</link>
		<comments>http://blog.osd.se/2007/08/12/helium/#comments</comments>
		<pubDate>Sun, 12 Aug 2007 14:57:18 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[Stupid]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/08/12/helium/</guid>
		<description><![CDATA[Ni vill inte titta på den här, tro mig. Man fastnar.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.weebls-stuff.com/toons/blimp">Ni vill inte titta på den här, tro mig. Man fastnar.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/08/12/helium/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ubisofts kvinnosyn</title>
		<link>http://blog.osd.se/2007/08/08/ubisofts-kvinnosyn/</link>
		<comments>http://blog.osd.se/2007/08/08/ubisofts-kvinnosyn/#comments</comments>
		<pubDate>Wed, 08 Aug 2007 11:00:54 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/08/08/ubisofts-kvinnosyn/</guid>
		<description><![CDATA[Ubisoft visar sig vara ett företag man bör undvika. Det är nästan så att man kan tro att det är ett skämt, men det finns en länk till en riktig pressrelease.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://kotaku.com/gaming/instruct-them/ubisoft-puts-women-in-their-place-287100.php">Ubisoft visar sig vara ett företag man bör undvika</a>. Det är nästan så att man kan tro att det är ett skämt, men det finns en länk till en riktig <a href="http://www.ubi.com/US/News/Info.aspx?nId=4662">pressrelease</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/08/08/ubisofts-kvinnosyn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mac eller PC?</title>
		<link>http://blog.osd.se/2007/08/08/mac-eller-pc/</link>
		<comments>http://blog.osd.se/2007/08/08/mac-eller-pc/#comments</comments>
		<pubDate>Wed, 08 Aug 2007 04:52:22 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/08/08/mac-eller-pc/</guid>
		<description><![CDATA[Just när jag trodde att jag hade sett alla Mac vs. PC-filmer. Lite ny var den i alla fall, men den är visst redan två månader gammal på Youtube.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.youtube.com/watch?v=Jkrn6ecxthM">Just när jag trodde att jag hade sett alla Mac vs. PC-filmer</a>. Lite ny var den i alla fall, men den är visst redan två månader gammal på Youtube.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/08/08/mac-eller-pc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Oskar on Rails</title>
		<link>http://blog.osd.se/2007/08/01/oskar-on-rails/</link>
		<comments>http://blog.osd.se/2007/08/01/oskar-on-rails/#comments</comments>
		<pubDate>Wed, 01 Aug 2007 19:45:21 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/08/01/oskar-on-rails/</guid>
		<description><![CDATA[Update : I was going to write a post about the changes I made to acts_as_taggable to get user support, but I just switched to has_many_polymorphs (HMP). I wanted self-referencing tags (being able to tag tags) and HMP provided that with little work. HMP also seems to work directly with will_paginate, which is great. The [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update :</strong> I was going to write a post about the changes I made to acts_as_taggable to get user support, but I just switched to <em>has_many_polymorphs</em> (HMP). I wanted self-referencing tags (being able to tag tags) and HMP provided that with little work. HMP also seems to work directly with will_paginate, which is great. The per user tags weren&#8217;t harder to get in HMP than in acts_as_taggable_on_steroids.</p>
<p>Oh the title, I&#8217;m so funny! I have to be the first one to ever think of that. Right&#8230;</p>
<p>This became another text in English, mainly because the follow up (if I write it) could possibly be useful to more people. We&#8217;ll see what happens.</p>
<p>Anyway, about a month ago, I decided to learn a bit of Ruby on Rails. I hadn&#8217;t tried Ruby before either, so learning Ruby was/is part of the process, but you can come a long way without having to study the language very deep. Most stuff works as you think they would, after learning the basics. In general I think it&#8217;s a nice language, but my knowledge isn&#8217;t very good yet.</p>
<p>Rails is wonderful to work with in general. I haven&#8217;t really done any web development in many years now (except for some minor WordPress hacking to modify it for my needs, but even that was more than a year ago, and a simple summer course two years ago). Back then I knew a lot less (doh!)  and most stuff I wrote was quite poorly written, I realize now, in terms of flexibility and security (Hello SQL injection!). I sure as hell wrote all my SQL statements by hand, though they were quite simple, and in general it usually became quite a mess quickly. I&#8217;m sure there existed some projects that the same idea as Rails, but I didn&#8217;t know about them then.</p>
<p>Something that is a bit annoying now, is that for my project I wanted two things; <a href="http://en.wikipedia.org/wiki/Taxonomy" title="Taxonomy">tags</a> and pagination (split up something over several pages, to give the user a better overview. Nothing fancy really). As it turns out, the built-in <a href="http://wiki.rubyonrails.com/rails/pages/PaginationHelper">paginate</a> module is deprecated and will be removed in the next version (it&#8217;s already gone from Edge Rails) and both the gem and plugin named <em>acts_as_taggable</em> are old and bad, in different ways (they aren&#8217;t the same).</p>
<p>The pagination wasn&#8217;t hard to solve. I found the plugin <em>will_paginate,</em> which works great. All good. For now&#8230;</p>
<p>The tagging wasn&#8217;t quite as easy. There are quite a few plugins that tries to (and does) fix what <em>acts_as_taggable</em> did, but better. One is <em><a href="http://blog.evanweaver.com/pages/has_many_polymorphs">has_many_polymorphs</a></em>, which is a fair bit more complex and flexible, but I didn&#8217;t really need that. The <a href="http://blog.evanweaver.com/articles/2007/01/13/growing-up-your-acts_as_taggable">guide he has, on his blog</a>, to use the plugin for tagging is about converting from <em>acts_as_taggable</em> to <em>has_many_polymorphs</em>, which includes a bit more work than it felt worth it for me. I ended up using <em>acts_as_taggable_on_steroids</em> (puh, long name) which is quite simple. It has (or had, I haven&#8217;t checked the last version yet) a bug with migrations, but in general it was easy to work with and to work around the bug.</p>
<p>I also wanted to tie my tags to users, because the project isn&#8217;t about a community and all user data is private to each user. There&#8217;s also a third plugin for tagging, <a href="http://geemus.devjavu.com/projects/geemus/wiki"><em>acts_as_taggable_redux</em></a>, and it has user support already, but it turns out that he ties the tagging (the connection between the tag and the item) to the user instead of the tag. That&#8217;s great for community like sites, like <a href="http://last.fm">last.fm,</a> where all tags are global, but that wasn&#8217;t what I wanted. I also found a post about a<a href="http://www.slashdotdash.net/articles/2006/04/29/acts_as_taggable-per-user-tagging">dding user support to<em> acts_as_taggable</em></a>, but it had the same user connections. Instead I decided that it was easy enough to do myself. I&#8217;m not saying that it&#8217;s hard, it&#8217;s not, but if someone else had done it already and had working plugin, I would rather use that).</p>
<p>It turned out to be even easier than I imagined at first. Rails has some really great tools. After adding a simple user_id column to the tag model, I could do things like</p>
<blockquote><p>user.tags.find_by_name(tag_name)</p></blockquote>
<p>which looks great in terms of readability. I might post a summary later on what I changed to get this working.</p>
<p>Now to the real problem: get them to work together. It wasn&#8217;t quite as easy to get <em>will_paginate</em> to work with <em>acts_as_taggable_on_steroids</em>. Find_tagged_with works directly on the type you want to tag, but will_paginate does that too. There might be easier ways than how I solved it, I don&#8217;t claim to be an expert after just a week (when I did this) with RoR. My solution wasn&#8217;t very nice either. I basically runs find_tagged_with, but returns the database request, instead of executing it and returning the result,  modify the SQL statement a bit and then use paginate_by_sql to turn it into a paginatable (cute word) object. The correct solution is probably to modify how <em>acts_as_taggable_on&#8230;</em> works and extend it with the things needed to paginate the results. If I understand the comments on the <em>acts_as_taggable_on_steroid</em> page, <a href="http://blog.wolfman.com/articles/2007/07/30/paginating-acts_as_taggable-with-will_paginate">someone might have done that now</a>. I have to read up on that.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/08/01/oskar-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stronger igen</title>
		<link>http://blog.osd.se/2007/07/07/stronger-igen/</link>
		<comments>http://blog.osd.se/2007/07/07/stronger-igen/#comments</comments>
		<pubDate>Sat, 07 Jul 2007 19:24:33 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Music]]></category>
		<category><![CDATA[Swedish]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/07/07/stronger-igen/</guid>
		<description><![CDATA[Nu blir det svenska.
MTV visar just nu videon till Kanye Wests låt Stronger. Jag misstänker att jag kommer att få se den många gånger från till, men det gör inget för det är en mycket bra låt.
]]></description>
			<content:encoded><![CDATA[<p>Nu blir det svenska.</p>
<p>MTV visar just nu videon till Kanye Wests låt <em>Stronger</em>. Jag misstänker att jag kommer att få se den många gånger från till, men det gör inget för det är en mycket bra låt.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/07/07/stronger-igen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The end?</title>
		<link>http://blog.osd.se/2007/07/03/the-end/</link>
		<comments>http://blog.osd.se/2007/07/03/the-end/#comments</comments>
		<pubDate>Tue, 03 Jul 2007 16:56:38 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Swedish]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/07/03/the-end/</guid>
		<description><![CDATA[I haven&#8217;t been very active at writing here the last month, as you can see if you look on the date of the previous post. I&#8217;m considering switching to writing in Swedish instead. I thought it would be fun to make it accessible for more people by writing in English, but it never felt really [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t been very active at writing here the last month, as you can see if you look on the date of the previous post. I&#8217;m considering switching to writing in Swedish instead. I thought it would be fun to make it accessible for more people by writing in English, but it never felt really good. So this is probably the end in a way. I haven&#8217;t decided if I&#8217;m going to start writing in Swedish, but we&#8217;ll see.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/07/03/the-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stronger</title>
		<link>http://blog.osd.se/2007/06/07/stronger/</link>
		<comments>http://blog.osd.se/2007/06/07/stronger/#comments</comments>
		<pubDate>Thu, 07 Jun 2007 15:57:10 +0000</pubDate>
		<dc:creator>Oskar</dc:creator>
				<category><![CDATA[Music]]></category>

		<guid isPermaLink="false">http://blog.osd.se/2007/06/07/stronger/</guid>
		<description><![CDATA[Stronger is one of the tracks on Kanye West&#8217;s coming album, but people are already going crazy over it since a radio show clip was ripped and spread on the internet. The clip featured a DJ talking over the awesome song, but there seems to be a full version out there now, spreading fast. The [...]]]></description>
			<content:encoded><![CDATA[<p><em>Stronger</em> is one of the tracks on Kanye West&#8217;s coming album, but people are already going crazy over it since a radio show clip was ripped and spread on the internet. The clip featured a DJ talking over the awesome song, but there seems to be a <a href="http://www.kanyetalk.com/">full version</a> <a href="http://normanrexwell.blogspot.com/2007/06/june-exclusives.html">out there now</a>, spreading fast. The song is based on Daft Punk&#8217;s <em>Harder Better Faster Stronger </em>and it&#8217;s quite intact actually. Kanye&#8217;s rap over it and his changes made are just perfect. I wasn&#8217;t too fond of his latest album, <em>Late Registratio</em>n, but I really liked <em>The College Dropout</em> and the new album, <em>Graduation,</em> looks very promising.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.osd.se/2007/06/07/stronger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
