<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Peter's Blog - Nodes for noob</title>
    <link>http://www.petersblog.org/</link>
    <description>Nodes containing the tag noob</description>
    <item>
      <title>Noob guide to the Rails Command Line</title>
      <link>http://www.petersblog.org/node/view/1575</link>
      <description>&lt;p&gt;
One of the things that is offputting about learning any of the web application stacks that I have tried (&lt;a href="/tag/rails"&gt;rails&lt;/a&gt;, &lt;a href="/tag/turbogears"&gt;turbogears&lt;/a&gt;, &lt;a href="/tag/django"&gt;django&lt;/a&gt;) is the command line operations that need to be performed. When you start using them they seem like magic but after a while they become old friends. Hence this article is a summary of the rails command line operations that I use routinely. 
&lt;/p&gt;
&lt;p&gt;
All these commands except for the first one should be executed in the root directory of your application. Note that for the commands that start 'script' you are running ruby scripts and under windows you will need to type 'ruby script/blah'. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;rails {app name}
&lt;/pre&gt;
&lt;p&gt;
The basic one, creates a new rails application. It will create a subdirectory with the given name in the current directory. This subdirectory is the root directory of your application and will be filled with all the appropriate files and subdirectories to start developing. It is a big code generator. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;script/server
&lt;/pre&gt;
&lt;p&gt;
This could be considered the next step after creating your application. You cd to the application's root directory and run this command. It will start a development web server and you will be able to connect to localhost:3000 in a web browser and admire your new creation. This is &lt;i&gt;the&lt;/i&gt; way to debug your application. Any &lt;a href="/node/1570"&gt;print statements&lt;/a&gt; will be dumped out here. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;script/generate model {model name}
&lt;/pre&gt;
&lt;p&gt;
This command will generate a new model in your application. A model is roughly equivalent to a table in the database. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;script/generate controller {controller name}
&lt;/pre&gt;
&lt;p&gt;
This command will generate a new controller in your application in the file 'app/controllers/{controller name}_controller.rb'. A controller is essentially the logic that takes a web page request and decides what to do with it. It will also create a directory called 'app/views/{controller name}' to store the views for this controller, views being the templates that determine how the web pages will look. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;rake db:migrate
&lt;/pre&gt;
&lt;p&gt;
This runs the migrations that will either define the database schema, modify it or undo those modifications. It defaults to updating the development database but can take the argument 'RAILS_ENV=production' to update the production database. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;script/console
&lt;/pre&gt;
&lt;p&gt;
script/console is useful for debugging. It gives you a ruby irb command line prompt where you can play with the inner workings of your code, primarily you have direct access to your models and hence the database. It can be an easier way of manipulating the database from a command line than typing SQL into the mysql or sqlite command line clients. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;rake --tasks
&lt;/pre&gt;
&lt;p&gt;
Lists what rake tasks are available. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;rake db:test:prepare
&lt;/pre&gt;
&lt;p&gt;
Prepares the test database for running unit tests. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;rake test
&lt;/pre&gt;
&lt;p&gt;
Runs all unit, functional and integration tests on your model. 
&lt;/p&gt;
&lt;p&gt;
I think that covers it. There are many more options than these are the ones that I have found to be most useful. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/rails"&gt;rails&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1575</guid>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">rails</category>
    </item>
    <item>
      <title>Ruby Metaclasses</title>
      <link>http://www.petersblog.org/node/view/1553</link>
      <description>&lt;p&gt;
Sometimes software jargon gets in the way of understanding what practical use something is. For example, you have a problem and you want to know how to solve it so where do you look it up in the ruby book? 
&lt;/p&gt;
&lt;p&gt;
What I am going to describe is based on the concept of 'metaclasses' which are classes for creating classes. If that jargon fried your brain then lets have a problem and an example. 
&lt;/p&gt;
&lt;p&gt;
I want to create classes for implementing Commands. Each Command class has a command number and I want to define this Command Number in the class definition but I don't want to wear my fingers out typing them in. What nice syntactic sugar can ruby offer to help me with this? 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; Class for building command classes&lt;/span&gt;
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt; &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;Command&lt;/span&gt;
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;   &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;&amp;lt;&amp;lt;&lt;/span&gt; self&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;     &lt;span class="Keyword"&gt;attr&lt;/span&gt; &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;nCommandNumber&lt;/span&gt;
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;     &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;CommandNumber&lt;/span&gt;(&lt;span class="Variable"&gt; nNumber&lt;/span&gt;)
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;       &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;nCommandNumber&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; nNumber
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;     &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  12 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
Ok, horrible syntax so what on earth is going on here? Well the effect of 'class &amp;lt;&amp;lt; &amp;lt;object&gt;' is to add new methods to an object so in this case we are adding a new attribute 'nCommandNumber' to hold our command number and a new method 'CommandNumber' so set its value to an object referred to by 'self'. 
&lt;/p&gt;
&lt;p&gt;
So what is 'self' equal to? Well it turns out that when you use this class (Command) to create another class (ACommand) then 'self' will hold the value of the 'ACommand' class object, i.e. the object that creates instances of ACommand objects. 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;&lt;/span&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; Declare a specific command&lt;/span&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;&lt;/span&gt;
&lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;ACommand&lt;span class="Superclass"&gt; &lt;span class="Superclass"&gt;&amp;lt;&lt;/span&gt; Command&lt;/span&gt;&lt;/span&gt;
  &lt;span class="Variable"&gt;CommandNumber&lt;/span&gt; &lt;span class="Constant"&gt;123&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
Hum, that looks easy. When ACommand is being defined the line 'CommandNumber 123' will result in the CommandNumber method in Command being executed on the ACommand class object so 'ACommand' will have an attribute called nCommandNumber added to it. We can see whether this happened with: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;print &lt;span class="Variable"&gt;ACommand&lt;/span&gt;.&lt;span class="Entity"&gt;nCommandNumber&lt;/span&gt;
=&amp;gt;&lt;span class="Constant"&gt;123&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
and indeed it works sweetly (which is what you would expect from syntactic sugar). It is important to note that it is the ACommand class itself that has these attributes, NOT objects CREATED by ACommand: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;oAC &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Variable"&gt;ACommand&lt;/span&gt;.&lt;span class="Entity"&gt;new&lt;/span&gt;
print oAC.&lt;span class="Entity"&gt;nCommandNumber&lt;/span&gt;
&lt;span class="Variable"&gt;NoMethodError&lt;/span&gt;: undefined method &lt;span class="String"&gt;&lt;span class="String"&gt;`&lt;/span&gt;nCommandNumber' for #&amp;lt;ACommand:0x345e180&amp;gt;&lt;/span&gt;
&lt;span class="String"&gt;        from (irb):15&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
so how can an object created by ACommand find the value of this attribute? 
&lt;/p&gt;
&lt;pre class="lazy"&gt;oAC.&lt;span class="Entity"&gt;class&lt;/span&gt;.&lt;span class="Entity"&gt;nCommandNumber&lt;/span&gt;
=&amp;gt; &lt;span class="Constant"&gt;123&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
one of the attributes of the object is the class that created it. 
&lt;/p&gt;
&lt;p&gt;
How would this be done in a lesser object orientated language? Probably using techniques such as: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;Command&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;GetCommandNumber&lt;/span&gt;
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;      &lt;span class="Keyword"&gt;raise&lt;/span&gt; &lt;span class="Variable"&gt;RuntimeError&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Pure base function called&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt; &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;ACommand&lt;span class="Superclass"&gt; &lt;span class="Superclass"&gt;&amp;lt;&lt;/span&gt; Command&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;GetCommandNumber&lt;/span&gt;
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;      &lt;span class="Keyword"&gt;return&lt;/span&gt; &lt;span class="Constant"&gt;123&lt;/span&gt;
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
or 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;Command&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;initialize&lt;/span&gt;(&lt;span class="Variable"&gt; nCommandNumber&lt;/span&gt;)
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;     &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;nCommandNumber&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; nCommandNumber
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt; &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;ACommand&lt;span class="Superclass"&gt; &lt;span class="Superclass"&gt;&amp;lt;&lt;/span&gt; Command&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;initialize&lt;/span&gt;
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;     &lt;span class="Keyword"&gt;super&lt;/span&gt;( &lt;span class="Constant"&gt;123&lt;/span&gt;)
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
or something similar. Like all syntactic sugar, it doesn't make the impossible possible (anything is possible in hand-coded assembler) it just makes for less typing and clearer code. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1553</guid>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">ruby</category>
    </item>
    <item>
      <title>Ruby != Python WRT default argument values</title>
      <link>http://www.petersblog.org/node/view/1548</link>
      <description>&lt;p&gt;
Found another ruby gotcha when calling methods declared with default argument values: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Entity"&gt;irb&lt;/span&gt;(main):&lt;span class="Constant"&gt;004&lt;/span&gt;:&lt;span class="Constant"&gt;0&lt;/span&gt;&lt;span class="Keyword"&gt;&amp;gt;&lt;/span&gt; &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;a&lt;/span&gt;(&lt;span class="Variable"&gt; b&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="Constant"&gt;1&lt;/span&gt;&lt;span class="Variable"&gt;,&lt;/span&gt;c&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="Constant"&gt;2&lt;/span&gt;&lt;span class="Variable"&gt;,&lt;/span&gt;d&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="Constant"&gt;3&lt;/span&gt;&lt;/span&gt;)
&lt;span class="Entity"&gt;irb&lt;/span&gt;(main):&lt;span class="Constant"&gt;005&lt;/span&gt;:&lt;span class="Constant"&gt;1&lt;/span&gt;&lt;span class="Keyword"&gt;&amp;gt;&lt;/span&gt; print &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;#{&lt;/span&gt;b&lt;span class="String"&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;#{&lt;/span&gt;c&lt;span class="String"&gt;}&lt;/span&gt;&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;#{&lt;/span&gt;d&lt;span class="String"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="Constant"&gt;\n&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="Entity"&gt;irb&lt;/span&gt;(main):&lt;span class="Constant"&gt;006&lt;/span&gt;:&lt;span class="Constant"&gt;1&lt;/span&gt;&lt;span class="Keyword"&gt;&amp;gt;&lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
=&amp;gt; &lt;span class="Constant"&gt;nil&lt;/span&gt;
&lt;span class="Entity"&gt;irb&lt;/span&gt;(main):&lt;span class="Constant"&gt;007&lt;/span&gt;:&lt;span class="Constant"&gt;0&lt;/span&gt;&lt;span class="Keyword"&gt;&amp;gt;&lt;/span&gt; a(c&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="Constant"&gt;23&lt;/span&gt;)
&lt;span class="Constant"&gt;23&lt;/span&gt; &lt;span class="Constant"&gt;2&lt;/span&gt; &lt;span class="Constant"&gt;3&lt;/span&gt;
=&amp;gt; &lt;span class="Constant"&gt;nil&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
A python programmer would have expected this to print 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Constant"&gt;2&lt;/span&gt; &lt;span class="Constant"&gt;23&lt;/span&gt; &lt;span class="Constant"&gt;3&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
as when calling the function we are saying we want c to be 23 and the other arguments to be left at their default values. Ruby appears to be ignoring the 'c=' bit when the function is being called so the first argument 'b' gets the value 23. 
&lt;/p&gt;
&lt;p&gt;
Pity, this python trick simplifies calling functions with lots of default parameters: you don't have to get the order of the parameters right or specify the correct default values of arguments before the one you are having to specify a value for. I should be calling this with: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Entity"&gt;irb&lt;/span&gt;(main):&lt;span class="Constant"&gt;007&lt;/span&gt;:&lt;span class="Constant"&gt;0&lt;/span&gt;&lt;span class="Keyword"&gt;&amp;gt;&lt;/span&gt; a(b&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="Constant"&gt;1&lt;/span&gt;, c&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="Constant"&gt;23&lt;/span&gt;)
&lt;/pre&gt;
&lt;p&gt;
i.e passing 'b=1' even though the declaration should tell ruby what the default for that should be. The 'b=' and 'c=' are only serving to help me selfdocument my code. 
&lt;/p&gt;
&lt;p&gt;
Ruby's behaviour is C/C++ish i.e. primitive. 
&lt;/p&gt;
&lt;p&gt;
UPDATE: 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://safari.oreilly.com/0596523696"&gt;O'Reilly Ruby Cookbook&lt;/a&gt;: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;fun_with_text&lt;/span&gt;(&lt;span class="Variable"&gt;text&lt;span class="Variable"&gt;,&lt;/span&gt; args&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="Variable"&gt;{&lt;/span&gt;&lt;span class="Variable"&gt;}&lt;/span&gt;&lt;/span&gt;)
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt;   text &lt;span class="Keyword"&gt;=&lt;/span&gt; text.&lt;span class="Entity"&gt;upcase&lt;/span&gt; &lt;span class="Keyword"&gt;if&lt;/span&gt; args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;upcase&lt;/span&gt;]
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;   text &lt;span class="Keyword"&gt;=&lt;/span&gt; text.&lt;span class="Entity"&gt;downcase&lt;/span&gt; &lt;span class="Keyword"&gt;if&lt;/span&gt; args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;downcase&lt;/span&gt;]
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;   &lt;span class="Keyword"&gt;if&lt;/span&gt; args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;find&lt;/span&gt;] &lt;span class="Keyword"&gt;and&lt;/span&gt; args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;replace&lt;/span&gt;]
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;     text &lt;span class="Keyword"&gt;=&lt;/span&gt; text.&lt;span class="Entity"&gt;gsub&lt;/span&gt;(args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;find&lt;/span&gt;], args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;replace&lt;/span&gt;])
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt;   text &lt;span class="Keyword"&gt;=&lt;/span&gt; text.&lt;span class="Entity"&gt;slice&lt;/span&gt;(&lt;span class="Constant"&gt;0&lt;/span&gt;, args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;truncate_at&lt;/span&gt;]) &lt;span class="Keyword"&gt;if&lt;/span&gt; args[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;truncate_at&lt;/span&gt;]
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;   &lt;span class="Keyword"&gt;return&lt;/span&gt; text
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt; &lt;span class="Entity"&gt;fun_with_text&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Foobar&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, {&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;upcase&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;true&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;truncate_at&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;5&lt;/span&gt;})
&lt;span class="line-numbers"&gt;  12 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; =&amp;gt; &amp;quot;FOOBA&amp;quot;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  13 &lt;/span&gt; &lt;span class="Entity"&gt;fun_with_text&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Foobar&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;upcase&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;true&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;truncate_at&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;5&lt;/span&gt;)
&lt;span class="line-numbers"&gt;  14 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; =&amp;gt; &amp;quot;FOOBA&amp;quot;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  15 &lt;/span&gt; &lt;span class="Entity"&gt;fun_with_text&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Foobar&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;find&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;o+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;replace&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;\1d&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;downcase&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;true&lt;/span&gt;)
&lt;span class="line-numbers"&gt;  16 &lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; =&amp;gt; &amp;quot;foodbar&amp;quot;&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/gotcha"&gt;gotcha&lt;/a&gt; &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1548</guid>
      <category domain="http://www.technorati.com/tag">gotcha</category>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">ruby</category>
    </item>
    <item>
      <title>Rails on Existing Tables</title>
      <link>http://www.petersblog.org/node/view/1546</link>
      <description>&lt;p&gt;
I'm going to blog this so I can remember it. How to get Rails/ActiveRecord to wrap an existing table in a database, i.e. one that you don't want created or manipulated via migrations: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;ruby script/generate model my_table --skip-migration
&lt;/pre&gt;
&lt;p&gt;
where my_table is the name of the table you are wrapping. This assumes you are already connected to the database (easy: edit config/database.yml). 
&lt;/p&gt;
&lt;p&gt;
It may be possible to simply derive a new class from ActiveRecord but the above is probably ensuring that everything is done properly. 
&lt;/p&gt;
&lt;p&gt;
In my case the database is being stuffed from some complex python code that I don't have the time/inclination to port to ruby/rails and I would rather the creation of the tables was still done through python. ActiveRecord being the wonder that it is will pick up the schema of the table and create wrappers automatically so you still don't need to tediously reiterate the column names/functions as you would in lesser frameworks. 
&lt;/p&gt;
&lt;p&gt;
It should be entirely possible to create views in MySQL and wrap them for Rails in this fashion although I found that creating views on a 20,000 record table causes the mysqld to take 99% cpu time for ten minutes afterwards. I know what you're saying, 'check your indexes', well I have. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/mysql"&gt;mysql&lt;/a&gt; &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/rails"&gt;rails&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1546</guid>
      <category domain="http://www.technorati.com/tag">mysql</category>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">rails</category>
    </item>
    <item>
      <title>Eclipse/CDT Make Broke</title>
      <link>http://www.petersblog.org/node/view/1545</link>
      <description>&lt;p&gt;
I've been looking into why the make facilities in my Eclipse/CDT install are not working (I try a make and nothing happens). I found a log file in my workspace folder called /.metadata/.log and the log file contains this: 
&lt;/p&gt;
&lt;div class="verbatim-block"&gt;&lt;pre&gt;!ENTRY org.eclipse.core.resources 4 2 2007-11-28 11:13:09.401
!MESSAGE Problems occurred when invoking code from plug-in: &amp;quot;org.eclipse.core.resources&amp;quot;.
!STACK 0
java.lang.NullPointerException
  at org.eclipse.cdt.managedbuilder.internal.core.CommonBuilder.build(CommonBuilder.java:520)
  at org.eclipse.cdt.managedbuilder.internal.core.CommonBuilder.build(CommonBuilder.java:506)
  at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:624)
  at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:166)
  at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:273)
  at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:354)
  at org.eclipse.core.internal.resources.Project.internalBuild(Project.java:494)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
So the java is hitting some runtime error and the architecture is so poor that the error is not reported to me, I have to sit there wondering why nothing is happening. 
&lt;/p&gt;
&lt;p&gt;
I searched through the eclipse plugin directory and found zip files that contained the source code. The error in the log pointed me to this line: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;IConfiguration&lt;/span&gt; activeCfg = info.getDefaultConfiguration();
&lt;/pre&gt;
&lt;p&gt;
so my guess was that getDefaultConfiguration was returning a null. This tied in with an observation I made that the "Build Configurations" menu in Eclipse/CDT had all it's items greyed out, including 'Manage..'. I decided that it was probably all because I hadn't set up my CDT project properly. I created a new CDT project, imported my source into it, deleted the old project, renamed the new project to match the old and tried it out. Voila, make ran and the output was in the console. 
&lt;/p&gt;
&lt;p&gt;
Lessons learnt: 
&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;
Projects may work with a number of perspectives (e.g. C/C++, Ruby etc) and each perspective wants it's configuration data within the project set up properly. At the same time, they seem only to set these configurations up when you create new projects, adding configuration data to an existing project when you use a new perspective appears to be up to the individual plugins: RDT, the ruby development plugin has an 'add ruby nature' option in the project menu to do this. 
&lt;/li&gt;
&lt;li&gt;
Because Eclipse is written in java it comes with all the source code. This means that even if some of it is a bit flaky one stands a good chance of working around the problems. 
&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;
Ok, I'm an Eclipse noob and didn't set my project up correctly but the environment really ought to have handled this better. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/eclipse"&gt;eclipse&lt;/a&gt; &lt;a href="/tag/java"&gt;java&lt;/a&gt; &lt;a href="/tag/noob"&gt;noob&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1545</guid>
      <category domain="http://www.technorati.com/tag">eclipse</category>
      <category domain="http://www.technorati.com/tag">java</category>
      <category domain="http://www.technorati.com/tag">noob</category>
    </item>
    <item>
      <title>Fun with Returning values from a Block to a Yield</title>
      <link>http://www.petersblog.org/node/view/1529</link>
      <description>&lt;p&gt;
I was trying to use blocks in &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt; and had code of this form: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt; &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;wangle&lt;/span&gt;
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;     &lt;span class="Keyword"&gt;while&lt;/span&gt; &lt;span class="Constant"&gt;true&lt;/span&gt;
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;         nValue &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Entity"&gt;rand&lt;/span&gt;(&lt;span class="Constant"&gt;10&lt;/span&gt;)
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;         &lt;span class="Keyword"&gt;if&lt;/span&gt; &lt;span class="Keyword"&gt;yield&lt;/span&gt;( nValue)
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;             &lt;span class="Keyword"&gt;return&lt;/span&gt;
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt;         &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;         print &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;THIS IS NEVER PRINTED&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;     &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  12 &lt;/span&gt; &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;wibble&lt;/span&gt;
&lt;span class="line-numbers"&gt;  13 &lt;/span&gt;     wangle &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;nValue&lt;/span&gt;|
&lt;span class="line-numbers"&gt;  14 &lt;/span&gt;         &lt;span class="Keyword"&gt;return&lt;/span&gt; nValue &lt;span class="Keyword"&gt;==&lt;/span&gt; &lt;span class="Entity"&gt;rand&lt;/span&gt;(&lt;span class="Constant"&gt;10&lt;/span&gt;)
&lt;span class="line-numbers"&gt;  15 &lt;/span&gt;     &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  16 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  17 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  18 &lt;/span&gt; &lt;span class="Entity"&gt;wibble&lt;/span&gt;()
&lt;/pre&gt;
&lt;p&gt;
The method 'wibble' calls 'wangle' passing it a block of code to execute. Wangle is supposed to keep calling this block until two random numbers match. if the numbers match it will return true and cause the loop to terminate. However, the loop will always terminate immediately, the string "THIS IS NEVER PRINTED" is never printed. 
&lt;/p&gt;
&lt;p&gt;
The reason for this is blindingly obvious in hindsight and after analysing the small print describing the yield command. This says "the value of the last expression evaluated in the block is passed back to the method as the value of the yield". Notice how it doesn't mention the 'return' keyword? This is because the return keyword is being applied to the outer method 'wibble' and that is returning to whatever called it, completely bypassing 'wangle' on it's way up the call tree. If 'wibble' is changed to: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;wibble&lt;/span&gt;
    wangle &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;nValue&lt;/span&gt;|
        nValue &lt;span class="Keyword"&gt;==&lt;/span&gt; &lt;span class="Entity"&gt;rand&lt;/span&gt;(&lt;span class="Constant"&gt;10&lt;/span&gt;)
    &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
then it will pass the results of the comparison "nValue == rand(10)" back to wangle to deal with as I originally intended. 
&lt;/p&gt;
&lt;p&gt;
I can imagine that this subtlety was permitted as there may be occasions when you want wibble to be able to return directly to it's caller. In my ruby noobyness I'm getting blocks mixed up with methods. Unfortunately I like using the return keyword, it makes code easier to skim read, it waves a big flag to me that says THIS IS THE RETURN VALUE. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/readthesmallprint"&gt;readthesmallprint&lt;/a&gt; &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1529</guid>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">readthesmallprint</category>
      <category domain="http://www.technorati.com/tag">ruby</category>
    </item>
    <item>
      <title>Linux Directory Sizes</title>
      <link>http://www.petersblog.org/node/view/1498</link>
      <description>&lt;p&gt;
This is all very noobish but it was about time I learned more about the du command. du means 'disk usage' and lists how much disk space is being used. At its most basic 
&lt;/p&gt;
&lt;pre class="lazy"&gt;du
&lt;/pre&gt;
&lt;p&gt;
dumps out a list of subdirectories under the current directory and shows their sizes in magic pixie units. First improvement is to show the sizes in something meaningful so we go 
&lt;/p&gt;
&lt;pre class="lazy"&gt;du -bh
&lt;/pre&gt;
&lt;p&gt;
to show sizes in 'human' readable units like Kbytes, Mbytes etc. 
&lt;/p&gt;
&lt;p&gt;
du normally dumps the sizes of files in subdirectories so we can go 
&lt;/p&gt;
&lt;pre class="lazy"&gt;du -bsh
&lt;/pre&gt;
&lt;p&gt;
to show just a sum total. 
&lt;/p&gt;
&lt;p&gt;
Now what if we find to our horror that a directory is using far more space than we thought, how to determine how big each subdirectory is? 
&lt;/p&gt;
&lt;pre class="lazy"&gt;du -bsh *
&lt;/pre&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/linux"&gt;linux&lt;/a&gt; &lt;a href="/tag/noob"&gt;noob&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1498</guid>
      <category domain="http://www.technorati.com/tag">linux</category>
      <category domain="http://www.technorati.com/tag">noob</category>
    </item>
    <item>
      <title>Ruby Notes 4</title>
      <link>http://www.petersblog.org/node/view/1460</link>
      <description>&lt;h3&gt;Syntactic Sugar&lt;/h3&gt;
&lt;p&gt;
Ruby has some perlisms such as 'unless'. This makes it possible to write code as follows: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;return&lt;/span&gt; &lt;span class="Keyword"&gt;unless&lt;/span&gt; bFlag
&lt;/pre&gt;
&lt;p&gt;
Without unless one may have to resort to: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;if&lt;/span&gt; &lt;span class="Keyword"&gt;!&lt;/span&gt;bFlag
  &lt;span class="Keyword"&gt;return&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
I think code is easier to read when lines start with keywords, when the keywords are further inside a line one has to rely on syntax highlighting to make the keyword shout out at you. This is especially important with a keyword like &lt;code&gt;return&lt;/code&gt; where you have to be aware that the code in the lines following may not be executed and understand the circumstances in which this will apply. 
&lt;/p&gt;
&lt;p&gt;
This way of expressing a statement always seems harder for me to get my head around, having written in C like languages for years. If one wrote similar expressions in English, which seems to be better expressed? 
&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;
jump up and down and sing a merry song if you are happy 
&lt;/li&gt;
&lt;li&gt;
if you are happy then jump up and down and sing a merry song 
&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;
To me the second form is even clearer as having the if at the start warns you in advance that a condition is coming up, it's not a surprise half way through the sentence. However, the second form requires you to type a whole extra word, and a four letter one at that! 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1460</guid>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">ruby</category>
    </item>
    <item>
      <title>has_and_belongs_to_many</title>
      <link>http://www.petersblog.org/node/view/1459</link>
      <description>&lt;p&gt;
Ooh Err, has_and_belongs_to_many, how_can_a_keyword_be_so_long? 
&lt;/p&gt;
&lt;p&gt;
Anyway, I've just used it for the first time to implement a UI in &lt;a href="/tag/rails"&gt;rails&lt;/a&gt; for user role permissions and here are some notes so that I might be able to use it again without excessive code trawling. 
&lt;/p&gt;
&lt;p&gt;
has_and_belongs_to_many is used to define many to many relationships. In the system I am implementing I have many Users and many Roles for them to perform. A User can be assigned to many Roles and a Role may be assigned to many Users. This is a classic many to many relationship and is implemented by having a table that contains a list of mappings of user id to role id, one mapping for each assignment of a user to a role. 
&lt;/p&gt;
&lt;p&gt;
This is (essentially) the migration that is used to create the user that is created by acts_as_authenticated but here it is stripped to the meaty bits: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;CreateUsers&lt;span class="Superclass"&gt; &lt;span class="Superclass"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Migration&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;self.up&lt;/span&gt;
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;     create_table &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;users&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;force&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;true&lt;/span&gt; &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;t&lt;/span&gt;|
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;       t.&lt;span class="Entity"&gt;column&lt;/span&gt; &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;login&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;string&lt;/span&gt;
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;     &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;self.down&lt;/span&gt;
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;     drop_table &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;users&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
This is the migration that the role_requirement plugin uses to create the roles tables: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;CreateRoles&lt;span class="Superclass"&gt; &lt;span class="Superclass"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Migration&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;self.up&lt;/span&gt;
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;     create_table &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;roles&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;t&lt;/span&gt;|
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;       t.&lt;span class="Entity"&gt;column&lt;/span&gt; &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;name&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;string&lt;/span&gt;
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;     &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt; &lt;span class="Comment"&gt;    &lt;span class="Comment"&gt;#&lt;/span&gt; generate the join table&lt;/span&gt;
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;     create_table &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;roles_users&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;id&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;false&lt;/span&gt; &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;t&lt;/span&gt;|
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;       t.&lt;span class="Entity"&gt;column&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;role_id&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;integer&lt;/span&gt;
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;       t.&lt;span class="Entity"&gt;column&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;user_id&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;integer&lt;/span&gt;
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt;     &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  12 &lt;/span&gt;     add_index &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;roles_users&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;role_id&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  13 &lt;/span&gt;     add_index &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;roles_users&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;user_id&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  14 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  15 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  16 &lt;/span&gt;   &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;self.down&lt;/span&gt;
&lt;span class="line-numbers"&gt;  17 &lt;/span&gt;     drop_table &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;roles&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  18 &lt;/span&gt;     drop_table &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;roles_users&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  19 &lt;/span&gt;   &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="line-numbers"&gt;  20 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
In summary, there are three tables in the database: 
&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;
A 'users' table with a field called 'login' that gives the user login name 
&lt;/li&gt;
&lt;li&gt;
A 'roles' table with a field called 'name' that gives the name of the role 
&lt;/li&gt;
&lt;li&gt;
A table called 'roles_users' that holds each assignment of a role to a user. 
&lt;/li&gt;&lt;/ul&gt;

&lt;p&gt;
Models need to be created for the User and Role table but not the roles_users table as that one is handled automagicaly by rails. 
&lt;/p&gt;
&lt;p&gt;
Here are the meaty bits of the model for the User table: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;User&lt;span class="Superclass"&gt; &lt;span class="Superclass"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;
  has_and_belongs_to_many &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;roles&lt;/span&gt;

  validates_length_of       &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;login&lt;/span&gt;,    &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;within&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;1&lt;/span&gt;..&lt;span class="Constant"&gt;40&lt;/span&gt;
  validates_uniqueness_of   &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;login&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;case_sensitive&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;false&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
And the model for the Role table: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="Entity"&gt;Role&lt;span class="Superclass"&gt; &lt;span class="Superclass"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;
  has_and_belongs_to_many &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;users&lt;/span&gt;

  validates_presence_of &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;name&lt;/span&gt;
  validates_uniqueness_of   &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;name&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;case_sensitive&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;false&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
Both these have the magical has_and_belongs_to_many declaration to invoke the many-to-many goodness. They also perform some validation on what the user enters such as making sure they are giving their users login names. 
&lt;/p&gt;
&lt;p&gt;
Now for some code for a partial that can be used in a view to edit or create user records. This is used to generate a list of check boxes, one for each role. The check box will be checked according to whether or not the user has been assigned that role: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &amp;lt;% form_for &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;user&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;url&lt;/span&gt; =&amp;gt; { &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;action&lt;/span&gt; =&amp;gt; action, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;id&lt;/span&gt; =&amp;gt; &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;user&lt;/span&gt;} &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;f&lt;/span&gt;| %&amp;gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt;     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;p&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Enter the login name for the new user:&lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;p&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;div&lt;/span&gt; &lt;span class="MetaTag"&gt;style&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;margin-left: 50px; margin-bottom: 50px&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;       &amp;lt;%= f.&lt;span class="Entity"&gt;text_field&lt;/span&gt;  &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;login&lt;/span&gt; %&amp;gt;
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt;     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;div&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt;     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;p&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Select the Roles that this user can perform&lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;p&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt;     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;div&lt;/span&gt; &lt;span class="MetaTag"&gt;style&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;margin-left: 50px&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  12 &lt;/span&gt;         &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;table&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  13 &lt;/span&gt;             &amp;lt;% &lt;span class="Keyword"&gt;for&lt;/span&gt; oRole &lt;span class="Keyword"&gt;in&lt;/span&gt; &lt;span class="Support"&gt;Role&lt;/span&gt;.&lt;span class="Entity"&gt;find&lt;/span&gt;(&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;all&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;order&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;name&lt;/span&gt;) %&amp;gt;
&lt;span class="line-numbers"&gt;  14 &lt;/span&gt;                 &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;tr&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  15 &lt;/span&gt;                     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;td&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  16 &lt;/span&gt;                         &amp;lt;%= check_box_tag &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;user[role_ids][]&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, oRole.&lt;span class="Entity"&gt;id&lt;/span&gt;, &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;user&lt;/span&gt;.&lt;span class="Entity"&gt;roles&lt;/span&gt;.&lt;span class="Entity"&gt;include?&lt;/span&gt;(oRole) %&amp;gt;
&lt;span class="line-numbers"&gt;  17 &lt;/span&gt;                     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;td&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  18 &lt;/span&gt;                     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MetaTag"&gt;td&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  19 &lt;/span&gt;                         &amp;lt;%= oRole.&lt;span class="Entity"&gt;name&lt;/span&gt; %&amp;gt;
&lt;span class="line-numbers"&gt;  20 &lt;/span&gt;                     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;td&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  21 &lt;/span&gt;                 &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;tr&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  22 &lt;/span&gt;             &amp;lt;% &lt;span class="Keyword"&gt;end&lt;/span&gt; %&amp;gt;
&lt;span class="line-numbers"&gt;  23 &lt;/span&gt;         &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;table&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  24 &lt;/span&gt;     &lt;span class="MetaTag"&gt;&lt;span class="MetaTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MetaTag"&gt;div&lt;/span&gt;&lt;span class="MetaTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;  25 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  26 &lt;/span&gt;     &amp;lt;%= submit_tag submit_tag %&amp;gt;
&lt;span class="line-numbers"&gt;  27 &lt;/span&gt;     &amp;lt;%= submit_tag &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Cancel&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; %&amp;gt;
&lt;span class="line-numbers"&gt;  28 &lt;/span&gt; &amp;lt;% &lt;span class="Keyword"&gt;end&lt;/span&gt; %&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Here we iterate through all the Roles in the role table, sorted into name order. For each role we generate a check box tag. The check_box_tag line is tricky but can be broken down as: 
&lt;/p&gt;
&lt;dl&gt;&lt;dt&gt;"user[role_ids][]"&lt;/dt&gt;&lt;dd&gt;
this is the name for the check box tag field. Naming the check box like this ensures that when the form is posted back to the server, the values for each tag will be placed correctly in the params array 
&lt;/dd&gt;
&lt;dt&gt;oRole.id&lt;/dt&gt;&lt;dd&gt;
the id of the Role record for this checkbox. It is these ids that are stored in the roles_users table to map a user id to a role id. 
&lt;/dd&gt;
&lt;dt&gt;@user.roles.include?(oRole)&lt;/dt&gt;&lt;dd&gt;
from the current user record, search the list of roles mapped to that user and see if the list already contains a particular role. If it does then the checkbox will be checked when the form is displayed. 
&lt;/dl&gt;&lt;/dd&gt;

&lt;p&gt;
When the form is submitted we need to ensure that the roles mapped to a user are updated correctly. This is simple: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="line-numbers"&gt;   1 &lt;/span&gt; &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;update&lt;/span&gt;
&lt;span class="line-numbers"&gt;   2 &lt;/span&gt;   strLogin &lt;span class="Keyword"&gt;=&lt;/span&gt; params[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;user&lt;/span&gt;][&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;login&lt;/span&gt;]
&lt;span class="line-numbers"&gt;   3 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;   4 &lt;/span&gt;   oUser &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Support"&gt;User&lt;/span&gt;.&lt;span class="Entity"&gt;find&lt;/span&gt;( params[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;id&lt;/span&gt;])
&lt;span class="line-numbers"&gt;   5 &lt;/span&gt;   oUser.&lt;span class="Entity"&gt;login&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; strLogin
&lt;span class="line-numbers"&gt;   6 &lt;/span&gt;   oUser.&lt;span class="Entity"&gt;role_ids&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; params[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;user&lt;/span&gt;][&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;role_ids&lt;/span&gt;]
&lt;span class="line-numbers"&gt;   7 &lt;/span&gt;   oUser.&lt;span class="Entity"&gt;save!&lt;/span&gt;
&lt;span class="line-numbers"&gt;   8 &lt;/span&gt;   flash[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;notice&lt;/span&gt;] &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Updated User '&lt;span class="String"&gt;&lt;span class="String"&gt;#{&lt;/span&gt;strLogin&lt;span class="String"&gt;}&lt;/span&gt;&lt;/span&gt;'&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class="line-numbers"&gt;   9 &lt;/span&gt; 
&lt;span class="line-numbers"&gt;  10 &lt;/span&gt;   redirect_to &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;action&lt;/span&gt; =&amp;gt; &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;list&lt;/span&gt;
&lt;span class="line-numbers"&gt;  11 &lt;/span&gt; &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
This is the method in the user controller that updates an existing record. The juicy bit is the line 
&lt;/p&gt;
&lt;pre class="lazy"&gt;oUser.&lt;span class="Entity"&gt;role_ids&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; params[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;user&lt;/span&gt;][&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;role_ids&lt;/span&gt;]
&lt;/pre&gt;
&lt;p&gt;
which is ALL it takes to update all the role assignments! This saves a lot of work such as adding new role assignments and removing extraneous ones. 
&lt;/p&gt;
&lt;p&gt;
Another little thing to watch out for: when deleting a role or a user do NOT use the delete method: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Support"&gt;User&lt;/span&gt;.&lt;span class="Entity"&gt;delete&lt;/span&gt;( params[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;id&lt;/span&gt;]) &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;# WRONG&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;code&gt;delete&lt;/code&gt; apparently just sends the raw sql to the database engine to get it to delete the object. Instead you should call &lt;code&gt;destroy&lt;/code&gt;: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Support"&gt;User&lt;/span&gt;.&lt;span class="Entity"&gt;destroy&lt;/span&gt;( params[&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;id&lt;/span&gt;]) &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; Delete user and database objects associated with him/her&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;code&gt;destroy&lt;/code&gt; will invoke the rails Active Record magic that will cause the roles_users table to be updated, removing any entires for roles or users that are being deleted. Calling &lt;code&gt;delete&lt;/code&gt; will not do this and will leave stray records in the database. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/rails"&gt;rails&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1459</guid>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">rails</category>
    </item>
    <item>
      <title>Ruby Notes 3</title>
      <link>http://www.petersblog.org/node/view/1455</link>
      <description>&lt;h3&gt;Endless Ends&lt;/h3&gt;
&lt;p&gt;
Ruby terminates just about everything with the &lt;code&gt;end&lt;/code&gt; keyword: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;SillyExample&lt;/span&gt;
  &lt;span class="Keyword"&gt;if&lt;/span&gt; a &lt;span class="Keyword"&gt;==&lt;/span&gt; &lt;span class="Constant"&gt;3&lt;/span&gt;
    &lt;span class="Constant"&gt;10&lt;/span&gt;.&lt;span class="Entity"&gt;repeat&lt;/span&gt; &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;n&lt;/span&gt;|
      &lt;span class="Keyword"&gt;if&lt;/span&gt; n &lt;span class="Keyword"&gt;==&lt;/span&gt; a
        print n
      &lt;span class="Keyword"&gt;end&lt;/span&gt;
    &lt;span class="Keyword"&gt;end&lt;/span&gt;
  &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
Maybe I am nesting too deep but the endless ends become quite confusing after a while, especially with ruby's official two space indents. Visual Basic 6 (ugh) at least has &lt;code&gt;end if&lt;/code&gt;, &lt;code&gt;loop&lt;/code&gt;, &lt;code&gt;end sub&lt;/code&gt; etc so you know what you are looking at the end of. 
&lt;/p&gt;
&lt;p&gt;
Then again, python has no equivalent to end: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="Entity"&gt;SillyExample&lt;/span&gt;():
    &lt;span class="Keyword"&gt;if&lt;/span&gt; a &lt;span class="Keyword"&gt;==&lt;/span&gt; &lt;span class="Constant"&gt;3&lt;/span&gt;:
        &lt;span class="Keyword"&gt;for&lt;/span&gt; i &lt;span class="Keyword"&gt;in&lt;/span&gt; &lt;span class="MetaFunctionCallPy"&gt;&lt;span class="Support"&gt;range&lt;/span&gt;&lt;span class="MetaFunctionCallPy"&gt;(&lt;/span&gt;&lt;span class="MetaFunctionCallPy"&gt;&lt;span class="Constant"&gt;10&lt;/span&gt;&lt;/span&gt;&lt;span class="MetaFunctionCallPy"&gt;)&lt;/span&gt;&lt;/span&gt;:
            &lt;span class="Keyword"&gt;if&lt;/span&gt; n &lt;span class="Keyword"&gt;==&lt;/span&gt; a:
                &lt;span class="Keyword"&gt;print&lt;/span&gt; n
&lt;/pre&gt;
&lt;p&gt;
but python's indentation standard is four spaces rather than two making it a bit easier to follow. 
&lt;/p&gt;
&lt;p&gt;
Maybe I should just defy convention and indent my ruby with four spaces? Who cares apart from the indentation Nazis? 
&lt;/p&gt;
&lt;h3&gt;More Scope for Errors&lt;/h3&gt;
&lt;p&gt;
So why wasn't this loop doing what I thought it would? 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Support"&gt;Mytable&lt;/span&gt;.&lt;span class="Entity"&gt;find&lt;/span&gt;( &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;all&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;order&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;serial_number&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;oRecord&lt;/span&gt;|
    print oRecord.&lt;span class="Entity"&gt;serial_number&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
Mytable is a &lt;a href="/tag/rails"&gt;rails&lt;/a&gt; ActiveRecord class and I'm using it to load records from a table. But nothing is printed although I am sure there are records there. 
&lt;/p&gt;
&lt;p&gt;
Hum, turns out I missed the call to the 'each' method: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Support"&gt;Mytable&lt;/span&gt;.&lt;span class="Entity"&gt;find&lt;/span&gt;( &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;all&lt;/span&gt;, &lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;order&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;serial_number&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).&lt;span class="Entity"&gt;each&lt;/span&gt; &lt;span class="Keyword"&gt;do &lt;/span&gt;|&lt;span class="Variable"&gt;oRecord&lt;/span&gt;|
    print oRecord.&lt;span class="Entity"&gt;serial_number&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;
and now it iterates through the recordset correctly. It seems that the first form silently does nothing, it doesn't seem to execute anything inside the block. It gives no errors either. 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/noob"&gt;noob&lt;/a&gt; &lt;a href="/tag/ruby"&gt;ruby&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1455</guid>
      <category domain="http://www.technorati.com/tag">noob</category>
      <category domain="http://www.technorati.com/tag">ruby</category>
    </item>
  </channel>
</rss>
