<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Technology &#38; Process Solutions</title>
	<atom:link href="http://heurionconsulting.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://heurionconsulting.wordpress.com</link>
	<description>Manuevering Thoughts to Solutions</description>
	<lastBuildDate>Thu, 17 Jan 2008 06:15:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='heurionconsulting.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Technology &#38; Process Solutions</title>
		<link>http://heurionconsulting.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://heurionconsulting.wordpress.com/osd.xml" title="Technology &#38; Process Solutions" />
	<atom:link rel='hub' href='http://heurionconsulting.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Rails Image Editor</title>
		<link>http://heurionconsulting.wordpress.com/2007/07/27/rails-image-editor/</link>
		<comments>http://heurionconsulting.wordpress.com/2007/07/27/rails-image-editor/#comments</comments>
		<pubDate>Fri, 27 Jul 2007 07:40:17 +0000</pubDate>
		<dc:creator>heurionconsulting</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://heurionconsulting.wordpress.com/2007/07/27/rails-image-editor/</guid>
		<description><![CDATA[&#160; INTRODUCTION Rails Ajax Image Editor is an ajaxified application for image manipulation using Rails technologies. This application has been derived from Peter Frueh (http://www.ajaxprogrammer.com) which originally existed in PHP. The PHP application has been modified to support Rails framework by Heurion Consulting (http://www.heurionconsulting.com) for the benefit of the Rails programmers to extend this application. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=8&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p align="left">&nbsp;</p>
<address><span style="font-size:12pt;"><u><b>INTRODUCTION<br />
</b></u></span></address>
<address><span style="font-size:10pt;"> Rails Ajax Image Editor is an ajaxified application for image manipulation using Rails technologies. This application has been derived from Peter Frueh <span> </span>(<a href="http://www.ajaxprogrammer.com/">http://www.ajaxprogrammer.com</a>) which originally existed in PHP. The PHP application has been modified to support Rails framework by Heurion Consulting (<a href="http://www.heurionconsulting.com/">http://www.heurionconsulting.com</a>) for the benefit of the Rails programmers to extend this application. </span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;">The original application is available at </span></address>
<address><span style="font-size:10pt;"><a href="http://ajaxian.com/archives/open-source-php-based-ajax-image-editor">http://ajaxian.com/archives/open-source-php-based-ajax-image-editor</a><span>  </span>for use</span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;">The features supported by this application are: </span></address>
<ul>
<li>
<address><span style="font-size:10pt;"> Loading an temporary image (which already exists in our server)</span></address>
</li>
<li>
<address><span style="font-size:10pt;">Perform Image cropping (using a crop area)</span></address>
</li>
<li>
<address><span style="font-size:10pt;">Perform Image Resize (with and without constraints)</span></address>
</li>
<li>
<address><span style="font-size:10pt;">Perform Rotation (90 degrees CW and CCW )</span></address>
</li>
<li>
<address><span style="font-size:10pt;">Save image</span></address>
</li>
<li>
<address><span style="font-size:10pt;">View Saved image </span></address>
</li>
<li><span style="font-size:10pt;">View Original Image</span></li>
</ul>
<address><span style="font-size:12pt;"> </span></address>
<address><span style="font-size:12pt;"><u><b>TECHNOLOGY<br />
</b></u></span></address>
<address><span style="font-size:10pt;"> This application has been built by and for Ruby on Rails Technology enthusiasts and also involves the following technologies:</span></address>
<ul>
<li>
<address><span style="font-size:10pt;">Ajax</span><span style="font-size:10pt;"> for postback activity</span></address>
</li>
<li>
<address><span style="font-size:10pt;">JSON(JavaScript object notation) for updating JavaScript image<br />
</span></address>
</li>
<li>
<address><span style="font-size:10pt;">Rmagick for the image manipulation. </span></address>
</li>
</ul>
<address><span style="font-size:12pt;"> </span></address>
<address><span style="font-size:12pt;"><u><b>CREDITS<br />
</b></u></span></address>
<address><span style="font-size:10pt;"> We would like to credit Mr. Peter Frueh <span> </span>(<a href="http://www.ajaxprogrammer.com/">http://www.ajaxprogrammer.com</a>) for having developed this application and also providing us the opportunity to modify the code to suit the Ruby on Rails needs.</span></address>
<address><span style="font-size:10pt;"> All credits of Java Script and CSS code belongs to Peter where as all Code of Ajaxification and Ruby on Rails Codes shall belong to Heurion Consulting (<a href="http://www.heurionconsulting.com/">http://www.heurionconsulting.com</a>).</span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;">This code is under LGPL license and you are allowed to do any modifications or extension to this code to suit your requirements. However you are not allowed to change the credits of either Mr. Peter Frueh (<a href="http://www.ajaxprogrammer.com/">http://www.ajaxprogrammer.com</a>) or Heurion Consulting (<a href="http://www.heurionconsulting.com/">http://www.heurionconsulting.com</a>) and they shall remain always.</span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;">This library is free software. You can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.</span></address>
<address><span style="font-size:10pt;">This library is distributed in the hope that it will be useful, but <font color="#3366ff">WITHOUT ANY WARRANTY</font>; without even the implied warranty of <font color="#3366ff">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE</font>.<span>  </span>See the GNU Lesser General Public License for more details.</span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:12pt;"><u><b><br />
DOWNLOAD AND SETUP<br />
</b></u></span></address>
<ul>
<li>
<address><span style="font-size:10pt;">Currently the wordpress does not give the option to download the Zip file. To receive the source code, please send an E-Mail to <a href="mailto:info@heurionconsulting.com">info@heurionconsulting.com</a></span></address>
</li>
<li>
<address><span style="font-size:10pt;"><span></span>Unzip the source  file to a suitable location</span></address>
</li>
<li>
<address><span style="font-size:10pt;"><span></span>Run “ruby script/server” (for windows) or ./script/server (for others)</span></address>
</li>
</ul>
<address><span style="font-size:12pt;"> </span></address>
<address><span style="font-size:12pt;"><u><b><br />
Code Walkthrough<br />
</b></u></span></address>
<address><span style="font-size:10pt;"> The main parts of the application are javascripts and the ruby controller. The notable files are explained as follows:</span></address>
<ul>
<li>
<address><span style="font-size:10pt;"> <font color="#3366ff"><b>ImageEditor.js</b></font> – Contains all the code that is necessary to do the image manipulations activity and also to send the post back to the server. This code uses Ajax.Request method to send data to the server. Also the Response is a JSON object which is used to update the client JavaScript image object.</span></address>
</li>
<li>
<address><span style="font-size:10pt;"><font color="#3366ff"><b>PageInfo.js</b></font> – Helper code for tracking mouse position, used to provide JavaScript Crop border.</span></address>
</li>
<li>
<address><span style="font-size:10pt;"><font color="#3366ff"><b>Imagecontroller_controller.rb</b></font> – Contains all server code where image manipulation is performed using RMagick and the resulting images are stored and picked from the respective (edit / active / original) folders.</span></address>
</li>
</ul>
<address><span style="font-size:10pt;"> Image Editor JavaScript uses Ajax.Request object to send all the information required for the modification of the image to the server. Along with the request all the parameters that are required to perform necessary image manipulation are sent. Once the server performs the operation and sends back the new results they are cached and the image is updated using JSON. </span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span> </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">newAjax.Request(&#8216;/imagecontroller/processImage&#8217;,</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span> </span>{</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"> synchronous:true, onSuccess: function(request)</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span><span></span>                      {</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>                                                                                     </span><span>   </span>if (request.readyState == 4                                                 </span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span><span></span><span></span>{</span>                                                                                                <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span> </span><span>       </span>var json = eval(&#8220;(&#8221; + request.responseText + &#8220;)&#8221;);</span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>     </span><span>         </span><span>      </span>if (json.imageFound) </span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>                                                </span><span>     </span>{</span>                                                                                            <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>     </span><span>     </span><span>   </span>ImageEditor.imageName = json.imageName;</span> <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>             </span><span>     </span><span></span><span></span>ImageEditor.w = json.w</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">                 </span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span><span></span><span></span>ImageEditor.h = json.h;</span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;">                                                     ImageEditor.loaderImage.setAttribute(&#8220;src&#8221;, <span>                  </span>&#8220;/images/edit/&#8221;+json.imageName);</span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>                              </span><span>    </span><span></span>} </span>                                                                                            <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span> </span><span>       </span>else </span>                                                                                    <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span> </span><span>         </span>{</span>   <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span><span></span>                                                                                    document.getElementById(&#8220;ImageEditorImage&#8221;)</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">               .innerHTML =</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span> </span>&#8216;&lt;span style=&#8221;font-<span>   </span>size:12px;</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">               color:red;&#8221;&gt; </span>                                                                             <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>       </span>Image was not found.&lt;/span&gt;&#8217;;</span>   <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>                                          </span><span></span>}</span>   <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>        </span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>                </span><span></span>}    </span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">        </span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span><span></span>},parameters:&#8221;imageName=&#8221; + ImageEditor.imageName            + ((args) ? </span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span><span></span>&#8220;&amp;&#8221; + args : &#8220;&#8221;)</span>   </address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">    })</span></address>
<address>  </address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"></span></address>
<address><span style="font-size:10pt;">  <font color="#3366ff">JSON(Java Script Object Notation)</font> allows us to represent complex objects in a format that can be easily parsed by the JavaScript interpreter. Once parsed, these objects can be interacted with ordinary JavaScript objects, with much less overhead than their XML counterparts. The downside of using JSON is that it is only understood by JavaScript interpreters, and therefore few server-side systems will have the ability to speak JSON easily. Thus, generating the data on the server will typically involve more overhead. Whichever format of data we choose, adopting a data-centric approach will require us to write some extra code to parse the response and decide what to do with it. Once the data to do the image manipulation is transferred in to the server we can utilize the RMagick supported by Ruby to perform operations of the image. </span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;"><span>     </span><b>P.S:</b> After each operation we save the image into a new name so that the new name is<span></span><span> </span>recognized by the html image tag and resets / updates to the new image.</span></address>
<address><span style="font-size:10pt;"> Henceforth on every modification we add a time stamp to the image name differentiates the old image with the new image.</span></address>
<address><span style="font-size:10pt;"> </span></address>
<address> </address>
<address><b><span style="font-size:10pt;"><font color="#3366ff">Part A: Initial Preperations</font></span></b></address>
<address><span style="font-size:10pt;">Before implementing the image, we have to setup initial preparations which are required to do the rest of the image operation. Some of them are identifying active / edit/ original directories, identifying the old and new image names. The following code snippets provides insight about each operation achieved inside the controller:</span></address>
<address><u><span style="font-size:10pt;"><font color="#3366ff"><b>Code Snippet:</b></font></span></u></address>
<address><span style="font-size:10pt;">#Registering rmagick</span></address>
<address><span style="font-size:10pt;">Either in the environment.rb file or on the start of the controller / model you can register the rmagick by using</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">require &#8216;rmagick&#8217;</span><br />
</address>
<address><span style="font-size:10pt;"><span>          </span></span></address>
<address><span style="font-size:10pt;"><span>  </span>#Create the directories</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>   </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">originalDirectory = &#8220;#{RAILS_ROOT}/public/images/original/&#8221;</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span> </span>   activeDirectory = &#8220;#{RAILS_ROOT}/public/images/active/&#8221;</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span> </span>       editDirectory = &#8220;#{RAILS_ROOT}/public/images/edit/&#8221;</span> </address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"></span></address>
<address><span style="font-size:10pt;"><span>    </span></span></address>
<address><span style="font-size:10pt;"><span>  </span>#Identify the original Image name</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>   </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">imageName = &#8216;parrots.jpg&#8217;</span> </address>
<address><span style="font-size:10pt;"><span>   </span></span></address>
<address><span style="font-size:10pt;"><span>  </span>#Prepare the new image name</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>   </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">extn = imageName.split(&#8216;.&#8217;).last</span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>   </span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>   </span>newimagename = imageName.gsub(/.jpg/,&#8221;)</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"></span></address>
<address><span style="font-size:10pt;"><span>     </span><span>    </span></span></address>
<address><span style="font-size:10pt;"><span>  </span>#new image name </span></address>
<address><font color="#3366ff"><span style="font-size:10pt;"><span>      </span></span></font><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">@image2Name = newimagename +&#8221;_&#8221;+                                Time.now.to_i.to_s+&#8221;.&#8221;+extn</font></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;"><span>  </span>#identify what action to be performed</span></address>
<address><span style="font-size:10pt;"><span>       </span></span><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">action = params[:actiontype]</font></span></address>
<address> </address>
<address><span style="font-size:10pt;"> </span></address>
<address><b><span style="font-size:10pt;"><font color="#3366ff">Part B: Final Preperations</font></span></b></address>
<address><span style="font-size:10pt;"> Once the images are ready and saved we have to update the javascript with these images. The controller shall get all information of the processed image and sends it to its corresponding view to prepare the JSON object.</span></address>
<address><u><span style="font-size:10pt;"><font color="#3366ff">Code Snippet:</font></span></u></address>
<address><span style="font-size:10pt;"> </span></address>
<address><b><span style="font-size:10pt;"><font color="#3366ff">Controller</font></span></b></address>
<address><span style="font-size:10pt;"> #check if the new image is saved, if not then replace the old image name to be the #new image. This occurs for View original and view active cases.</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span> </span>    <font color="#3366ff">unless File.exists?(File.join(editDirectory,@image2Name))</font></span></address>
<address><font color="#3366ff"><span style="font-size:10pt;font-family:Verdana;color:red;"><span>     </span></span></font><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">@image2Name = imageName</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff"><span>     </span>end</font></span></address>
<address><span style="font-size:10pt;"><span>  </span></span></address>
<address><span style="font-size:10pt;">  #get the image and it positions and serialize them to the view<span>    </span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>     </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">img = with_image()</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>     </span>@w= img.columns</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>     </span>@h= img.rows</span></address>
<address> </address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><b><span style="font-size:10pt;"><font color="#3366ff">View</font></span></b></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;">   <font color="#3366ff">{imageFound:true,imageName:&#8221;&lt;%= @image2Name                %&gt;&#8221;,x:&lt;%= @x %&gt;,y:&lt;%= @y %&gt;,w:&lt;%= @w %&gt;,h:</font></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">    &lt;%= @h %&gt;}</font></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;"><b><font color="#3366ff">JSON Variables</font></b></span></address>
<ul>
<li>
<address><span style="font-size:10pt;">ImageFound : Boolean test that the view was called</span></address>
</li>
<li>
<address><span style="font-size:10pt;">ImageName: Holds the new image name</span></address>
</li>
<li>
<address><span style="font-size:10pt;">X: top left- x position of the new image</span></address>
</li>
<li>
<address><span style="font-size:10pt;">Y: top left – y position of the new image</span></address>
</li>
<li>
<address><span style="font-size:10pt;">W: width of the new image</span></address>
</li>
<li>
<address><span style="font-size:10pt;">H: height of the new image</span></address>
</li>
</ul>
<address><span style="font-size:10pt;"> <font color="#3366ff"><b>Part C: Performing Operations (the mid part)<br />
</b></font></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;"><font color="#3366ff">a) View Original</font></span></address>
<address><span style="font-size:10pt;"> <span>   </span># copies the image from original directory to edit directory to                  show the image from the edit directory</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>        </span><font color="#3366ff">FileUtils.cp(originalDirectory+imageName,                              editDirectory+imageName)</font></span></address>
<address> </address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;"><font color="#3366ff">b) View Active</font></span></address>
<address><span style="font-size:10pt;"> <span>  </span># copies the image from active directory to edit directory to show         the image from<span>  </span></span></address>
<address><span style="font-size:10pt;"><span>   </span># the edit directory</span></address>
<address><span style="font-size:10pt;"><span>           </span></span><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">FileUtils.cp(activeDirectory+imageName,                                editDirectory+@image2Name)</font></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address> </address>
<address><span style="font-size:10pt;"><font color="#3366ff">c) Save as Active</font></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>      <font color="#3366ff">i</font></span><font color="#3366ff"><span></span>mg = with_image()</font></span></address>
<address><span style="font-size:10pt;"><span>   </span># saving the image with the time stamp in edit directory</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>     </span><font color="#3366ff">save_image(img,@image2Name)</font></span></address>
<address><span style="font-size:10pt;"><span>   </span># writing that image in active directory</span></address>
<address><span style="font-size:10pt;"><span>        </span></span><span style="font-size:10pt;font-family:'Times New Roman';"><span> </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">img.write(File.join(&#8220;#{RAILS_ROOT}</span><font color="#3366ff"><span style="font-size:10pt;font-family:Verdana;color:red;"><br />
</span></font></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">     /public/images/active/parrots.jpg&#8221;))</font></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address> </address>
<address><span style="font-size:10pt;"><font color="#3366ff">d) Resize</font></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>     </span><font color="#3366ff">out_w= params[:w].to_i</font></span></address>
<address><font color="#3366ff"><span style="font-size:10pt;font-family:Verdana;color:red;"><span>     </span></span></font><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">out_h = params[:h].to_i</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff"><span>     </span>img = with_image()</font></span></address>
<address><span style="font-size:10pt;"><span>   </span># resizing the image by the given dimensions</span></address>
<address><span style="font-size:10pt;"><span>      </span><font color="#3366ff">img = img.resize(out_w,out_h)</font></span></address>
<address><span style="font-size:10pt;">#saving the resized  image to active folder</span></address>
<address><span style="font-size:10pt;color:red;"><span>         </span></span><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">save_image(img,@image2Name)</font></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address> </address>
<address><span style="font-size:10pt;"><font color="#3366ff">e) Crop</font></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;">  <span>  </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">@x = params[:x].to_i</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>    </span>@y = params[:y].to_i</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>    </span>@w = params[:w].to_i</span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>    </span>@h = params[:h].to_i</span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>  </span>img = with_image()</span>  <span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>    </span>unless @w == 0 or @h == 0</span></address>
<address> </address>
<address><span style="font-size:10pt;"><span> </span># cropping the image by its x and y postions with width and height</span></address>
<address><span style="font-size:10pt;"><span>        </span></span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">img = img.crop(@x,@y,@w,@h)</span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span></span></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:#3366ff;"><span>    </span><span></span>  end</span> </address>
<address><font color="#3366ff"><span style="font-size:10pt;font-family:Verdana;color:red;"></span></font></address>
<address><span style="font-size:10pt;"><span></span># saving the croped image to active folder</span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>   </span><span></span><span>  </span><font color="#3366ff">save_image(img,@image2Name)</font></span></address>
<address><span style="font-size:10pt;"><span>        </span></span></address>
<address> </address>
<address><span style="font-size:10pt;"><font color="#3366ff">f) Rotate</font></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><span>      </span><font color="#3366ff">degrees = params[:degrees].to_i</font></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff"><span>      </span>img = with_image()</font></span></address>
<address><span style="font-size:10pt;"><span></span><span></span># rotaing the image by particular degrees</span></address>
<address><span style="font-size:10pt;"><span>          </span></span><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">img=img.rotate!(degrees)</font></span></address>
<address><span style="font-size:10pt;"><span></span># saving the rotated image to active folder</span></address>
<address><span style="font-size:10pt;"><span>          </span></span><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">save_image(img,@image2Name)</font></span></address>
<address><span style="font-size:12pt;"> </span></address>
<address><span style="font-size:12pt;"><u><b><br />
EXTENSIONS TO CODE<br />
</b></u></span></address>
<ul>
<li>
<address><span style="font-size:10pt;"> In this application the image shown in the browser is already in the server. We are taking a single image and modifying around it. We can extend this application by loading user images by using any standard upload methodology. One of the notable </span><span style="font-size:10pt;">Ajax</span><span style="font-size:10pt;"> upload feature is what is available in the photo gallery application available in the book “</span><span style="font-size:10pt;">Ajax</span><span style="font-size:10pt;"> on Rails” by O’reilly Publications. The code for uploading files from client side is:</span></address>
</li>
</ul>
<address>
<p class="MsoNormal" style="margin-left:0.25in;text-align:justify;"><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">&lt;div class=&#8221;upload_container&#8221;&gt;<br />
&lt;% form_for :photo, Photo.new,<br />
:url =&gt; photos_url(:usertemplate_id =&gt; @usertemplate),<br />
:html =&gt; { :multipart =&gt; true, :target =&gt; &#8220;uploader&#8221;,<br />
:id =&gt; &#8220;photo_upload&#8221; } do |f| %&gt;<br />
&lt;%= f.file_field :file, :size=&gt;&#8221;17&#8243; %&gt;&lt;br /&gt;<br />
&lt;%= submit_tag &#8220;Upload&#8221;,:onClick =&gt; &#8220;Photo.upload();&#8221;     %&gt;<br />
&lt;%= hidden_field_tag &#8220;photo_id&#8221;, photoposition %&gt;<br />
&lt;% end %&gt;<br />
&lt;/div&gt;</span></p>
</address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"></span></address>
<ul>
<li>
<address><span style="font-size:10pt;">In this application we are currently providing editing the image by crop, resize and rotate using Rmagick. Image rotation is done for 90 degrees clockwise and anticlockwise directions only. We can extend this application by rotating the image in any degrees by Rmagick.</span></address>
</li>
</ul>
<address><span style="font-size:10pt;font-family:Verdana;color:red;">  <font color="#3366ff"> </font><font color="#3366ff">img = Magick::Image::read(File.join(Directory,file)).first</font></span></address>
<address><span style="font-size:10pt;font-family:Verdana;color:red;"><font color="#3366ff">   img.rotate!(degrees)</font></span></address>
<address><span style="font-size:10pt;"> </span></address>
<address><span style="font-size:10pt;"><b> P.S:</b> Note when you are rotating the image, background changes into whitespace. It can be removed by Rmagick    </span><span style="font-size:10pt;font-family:Verdana;color:#3366ff;">img.matte_floodfill(x,y)</span><span style="font-size:10pt;"> concept. (Not supported in IE6)</span></address>
<ul>
<li>
<address><span style="font-size:10pt;">In this application we are currently resizing the image by giving values in the text. We can extend this application by using grips on the edges of image instead of using text. We can resize the image by dragging the grips in and out.</span></address>
</li>
</ul>
<p><font color="#b84f70"><b>- HEURION CONSULTING INFORMATION RELEASE</b></font></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/heurionconsulting.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/heurionconsulting.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/heurionconsulting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/heurionconsulting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/heurionconsulting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/heurionconsulting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/heurionconsulting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/heurionconsulting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/heurionconsulting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/heurionconsulting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/heurionconsulting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/heurionconsulting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/heurionconsulting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/heurionconsulting.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/heurionconsulting.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/heurionconsulting.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=8&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://heurionconsulting.wordpress.com/2007/07/27/rails-image-editor/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/023d73a0417c34c19195a4dc9b55d9a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">heurionconsulting</media:title>
		</media:content>
	</item>
		<item>
		<title>Using belongs_to to handle multiple Parent Records in a single child record</title>
		<link>http://heurionconsulting.wordpress.com/2007/05/29/using-belongs_to-to-connect-different-classes/</link>
		<comments>http://heurionconsulting.wordpress.com/2007/05/29/using-belongs_to-to-connect-different-classes/#comments</comments>
		<pubDate>Tue, 29 May 2007 06:40:35 +0000</pubDate>
		<dc:creator>heurionconsulting</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://heurionconsulting.wordpress.com/2007/05/29/using-belongs_to-to-connect-different-classes/</guid>
		<description><![CDATA[This is a very small post where I shall explain some additional features of belongs_to which I myself didn’t know about until I saw a requirement. Generally belongs_to in common sense is used along with has_one or has_many associations to associate relationships between 2 tables where user can use the information. So in the basic [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=7&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:9pt;font-family:Verdana;">This is a very small post where I shall explain some additional features of <span style="color:blue;">belongs_to</span> which I myself didn’t know about until I saw a requirement.</span><span style="font-size:9pt;font-family:Verdana;"> </span><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;">Generally <span style="color:blue;">belongs_to</span> in common sense is used along with <span style="color:blue;">has_one</span> or <span style="color:blue;">has_many</span> associations to associate relationships between 2 tables where user can use the information.</span><span style="font-size:9pt;font-family:Verdana;"> </span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">So in the basic sense, whenever there is a need to connect Parent and Child tables with <span style="color:blue;">belongs_to</span> and <span style="color:blue;">has_many</span> relationship, developers generally use the following known table relationship i.e.</span><span style="font-size:9pt;font-family:Verdana;"> </span><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;">Database Information.</span><span style="font-size:9pt;font-family:Verdana;"> </span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;"><span>1.<span style="font:7pt 'Times New Roman';"> </span></span></span><span style="font-size:9pt;font-family:Verdana;">Create the <span style="color:blue;">Parents</span> table with ID. </span><span style="font-size:9pt;font-family:Verdana;"><span></span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;"><span>2.<span style="font:7pt 'Times New Roman';"> </span></span></span><span style="font-size:9pt;font-family:Verdana;">Create the <span style="color:blue;">Children</span> table with ID.</span><span style="font-size:9pt;font-family:Verdana;"><span></span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;"><span>3.<span style="font:7pt 'Times New Roman';"> </span></span></span><span style="font-size:9pt;font-family:Verdana;">Add a Field by name <span style="color:blue;">parent_id</span> in children table</span><span style="font-size:9pt;font-family:Verdana;"> </span><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;">Model Information.</span><span style="font-size:9pt;font-family:Verdana;"> </span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">In Parent model, add an association to the child model.</span></p>
<p><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;"><font face="Times New Roman"><span></span></font></span><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';">class</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> Parent &lt; ActiveRecord::Base</span><span style="font-size:9pt;color:black;font-family:'Courier New';"><span><br />
</span>has_many </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:children<br />
</span></strong><span style="font-size:9pt;font-family:'Courier New';"></span><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';">end</span></strong></p>
<p><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';"></span></strong><span style="font-size:9pt;"></span><span style="font-size:9pt;"></span><span style="font-size:9pt;"><span style="font-size:9pt;font-family:Verdana;">In Child model, add an association to parent model.</span><span style="font-size:9pt;"><font face="Times New Roman"> </font></span></span></p>
<p><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';">class</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> Child &lt; ActiveRecord::Base</span><span style="font-size:9pt;color:black;font-family:'Courier New';"><span></span><br />
belongs_to </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:parent<br />
</span></strong><span style="font-size:9pt;font-family:'Courier New';"></span><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';">end</span></strong><span style="font-size:9pt;"></span><span style="font-size:9pt;"><font face="Times New Roman"> </font></span><span style="font-size:9pt;"><span style="font-size:9pt;font-family:Verdana;"></span></span></p>
<p><span style="font-size:9pt;"><span style="font-size:9pt;font-family:Verdana;">This is a basic idea of using a <span style="color:blue;">belongs_to</span> and <span style="color:blue;">has_many</span> relationships and solves most of the problems used in rails programming.</span><span style="font-size:9pt;font-family:Verdana;"> </span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">However, when there exists that multiple connectivity is required in a particular <span style="color:blue;">Parent</span></span><span style="font-size:9pt;color:blue;font-family:Verdana;">-Child</span><span style="font-size:9pt;font-family:Verdana;"> relationship, then this concept does not work.</span><span style="font-size:9pt;font-family:Verdana;"> </span><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;">Considering the above example, the requirement says that for any child record in the children table, there can exist only one parent table. Sometimes there exists a requirement of more than one entry e.g. Mother and Father is a parent for a child. So when there is a requirement to have a child record created with 2 foreign keys of Parents table i.e.<span> </span>one for <span style="color:blue;">father_id</span> and another for <span style="color:blue;">mother_id</span>, the above concept does not work.</span><span style="font-size:9pt;font-family:Verdana;"> </span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">In this kind of relationship, the solution is to modify the above requirement as</span><span style="font-size:9pt;font-family:Verdana;"> </span><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;">Database Information.</span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;font-family:Verdana;"><span>1.<span style="font:7pt 'Times New Roman';"> </span></span></span><span style="font-size:9pt;font-family:Verdana;">Create the <span style="color:blue;">Parents</span> table with ID.</span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;font-family:Verdana;"><span>2.<span style="font:7pt 'Times New Roman';"> </span></span></span><span style="font-size:9pt;font-family:Verdana;">Create the <span style="color:blue;">Children </span>table with ID.</span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;font-family:Verdana;"><span>3.<span style="font:7pt 'Times New Roman';"> </span></span></span><span style="font-size:9pt;font-family:Verdana;">Add 2 fields by name <span style="color:blue;">father_id</span> and <span style="color:blue;">mother_id </span>in the children table.</span><span style="font-size:9pt;font-family:Verdana;"> </span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">Model Information</span><span style="font-size:9pt;font-family:Verdana;"> </span><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;">in Parent model adds an association to the child model.<font face="Times New Roman"> </font></span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;"><font face="Times New Roman"><span></span></font></span><span style="font-size:9pt;"><span></span></span><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';">class</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> Parent &lt; ActiveRecord::Base<br />
</span><span style="font-size:9pt;font-family:'Courier New';"></span><span style="font-size:9pt;color:black;font-family:'Courier New';"><span></span><span></span>has_many </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:children</span></strong><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';"><br />
end</span></strong><span style="font-size:9pt;"></span><span style="font-size:9pt;"><font face="Times New Roman"> </font></span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">Model information in Child model adds an association to parent model.</span></p>
<p><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;"></span><span style="font-size:9pt;"><span style="font-size:9pt;"><span></span></span><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';">class</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> Child &lt; ActiveRecord::Base<br />
</span><span style="font-size:9pt;font-family:'Courier New';"></span><span style="font-size:9pt;color:black;font-family:'Courier New';"><span></span><span></span></span></span></p>
<p><span style="font-size:9pt;"><span style="font-size:9pt;color:black;font-family:'Courier New';">belongs_to </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:father</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">, </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:class_name</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> =&gt; </span><span style="font-size:9pt;color:#2a00ff;font-family:'Courier New';">&#8220;Parent&#8221;</span><span style="font-size:9pt;color:black;font-family:'Courier New';">, </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:foreign_key</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> =&gt; </span><span style="font-size:9pt;color:#2a00ff;font-family:'Courier New';">&#8220;father_id&#8221;</span><span style="font-size:9pt;font-family:'Courier New';"></span><span style="font-size:9pt;color:black;font-family:'Courier New';"><span><br />
</span><span></span>belongs_to </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:mother</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">, </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:class_name</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> =&gt; </span><span style="font-size:9pt;color:#2a00ff;font-family:'Courier New';">&#8220;Parent&#8221;</span><span style="font-size:9pt;color:black;font-family:'Courier New';">, </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:foreign_key</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> =&gt; </span><span style="font-size:9pt;color:#2a00ff;font-family:'Courier New';">&#8220;mother_id&#8221;</span></span></p>
<p><span style="font-size:9pt;"><span style="font-size:9pt;color:#2a00ff;font-family:'Courier New';"></span><span style="font-size:9pt;font-family:'Courier New';"></span><strong><span style="font-size:9pt;color:#a4357a;font-family:'Courier New';">end</span></strong><span style="font-size:9pt;"></span><strong><span style="font-size:9pt;"><font face="Times New Roman"> </font></span></strong></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">Another typical example to explain this need is in a shopping cart where you would have an order attached to 2 different addresses such as shipping address and billing address, where both are of type address.</span><span style="font-size:9pt;font-family:Verdana;"> </span><span style="font-size:9pt;font-family:Verdana;"><span style="font-size:9pt;font-family:Verdana;">In the example, the subject of interest is using the <span style="color:blue;">belongs_to</span> methodology. Let us investigate what the above sentence mean.</span><span style="font-size:9pt;"><font face="Times New Roman"> </font></span></span></p>
<p><span style="font-size:9pt;color:black;font-family:'Courier New';">belongs_to </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:father</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">, </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:class_name</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">=&gt;</span><span style="font-size:9pt;color:#2a00ff;font-family:'Courier New';">&#8220;Parent&#8221;</span><span style="font-size:9pt;color:black;font-family:'Courier New';">, </span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:foreign_key</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">=&gt;</span><span style="font-size:9pt;color:#2a00ff;font-family:'Courier New';">&#8220;father_id&#8221;</span><span style="font-size:9pt;"></span><span style="font-size:9pt;"></span></p>
<p><span style="font-size:9pt;"></span><span style="font-size:9pt;"><span style="font-size:9pt;font-family:Verdana;">When we say </span><span style="font-size:9pt;color:black;font-family:Verdana;">belongs_to </span><strong><span style="font-size:9pt;color:#ff4040;font-family:Verdana;">:father</span></strong><span style="font-size:9pt;font-family:Verdana;">, we mean that you can use the keyword </span><strong><span style="font-size:9pt;color:#ff4040;font-family:Verdana;">father</span></strong><span style="font-size:9pt;font-family:Verdana;"> to get information about the father object i.e. </span><span style="font-size:9pt;"></span></span></p>
<p><strong><span style="font-size:9pt;color:#004080;font-family:'Courier New';"><span></span>@child</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> = params[</span><strong><span style="font-size:9pt;color:#ff4040;font-family:'Courier New';">:selectedchild</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">]</span><span style="font-size:9pt;font-family:'Courier New';"></span><span style="font-size:9pt;color:black;font-family:'Courier New';"><span></span></span><strong><span style="font-size:9pt;color:#004080;font-family:'Courier New';"><br />
@father</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';"> = </span><strong><span style="font-size:9pt;color:#004080;font-family:'Courier New';">@child</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">.father<br />
</span><span style="font-size:9pt;font-family:'Courier New';"></span><span style="font-size:9pt;color:black;font-family:'Courier New';"><span></span></span><strong><span style="font-size:9pt;color:#004080;font-family:'Courier New';">@father</span></strong><span style="font-size:9pt;color:black;font-family:'Courier New';">&#8230;. </span><span style="font-size:9pt;color:#3f7f5f;font-family:'Courier New';">#Get father details&#8230;.</span></p>
<p><span style="font-size:9pt;color:#3f7f5f;font-family:'Courier New';"></span><span style="font-size:9pt;"></span><span style="font-size:9pt;"><span style="font-size:9pt;font-family:Verdana;">Since there does not exist any table as father, we explicitly instruct the Model to consider this <strong><font color="#ff0000">Father</font></strong> object to be an entry in the Parent object (table) and could be accessed by the <span style="color:blue;">foreign_key</span> </span><span style="font-size:9pt;color:#2a00ff;font-family:Verdana;">father_id</span><span style="font-size:9pt;font-family:Verdana;"> of the children table.</span><span style="font-size:9pt;font-family:Verdana;"> </span></span></p>
<p><span style="font-size:9pt;font-family:Verdana;">Hope this post explains how to solve multiple elements of a parent within a child record.</span></p>
<p><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;font-family:Verdana;"></span><span style="font-size:9pt;font-family:Verdana;"><strong><span style="font-size:9pt;font-family:Verdana;">&#8211; Heurion Consulting Information Release</span></strong></span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/heurionconsulting.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/heurionconsulting.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/heurionconsulting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/heurionconsulting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/heurionconsulting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/heurionconsulting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/heurionconsulting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/heurionconsulting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/heurionconsulting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/heurionconsulting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/heurionconsulting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/heurionconsulting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/heurionconsulting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/heurionconsulting.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/heurionconsulting.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/heurionconsulting.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=7&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://heurionconsulting.wordpress.com/2007/05/29/using-belongs_to-to-connect-different-classes/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/023d73a0417c34c19195a4dc9b55d9a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">heurionconsulting</media:title>
		</media:content>
	</item>
		<item>
		<title>Using FIND Methods in RAILS::ACTIVERECORD</title>
		<link>http://heurionconsulting.wordpress.com/2007/05/24/using-find-methods-in-railsactiverecord/</link>
		<comments>http://heurionconsulting.wordpress.com/2007/05/24/using-find-methods-in-railsactiverecord/#comments</comments>
		<pubDate>Thu, 24 May 2007 10:42:24 +0000</pubDate>
		<dc:creator>heurionconsulting</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://heurionconsulting.wordpress.com/2007/05/24/using-find-methods-in-railsactiverecord/</guid>
		<description><![CDATA[All of us know that we can use multiple Find methods to get information from the database via models and we also know that this is possible because Active Record::Base provides us the support for the same. Some of the normal ones which are most used by developers are find(id), find_all and find_first. All these [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=6&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><font size="1">All of  us know that we can use multiple<font color="#3366ff"> <strong>Find</strong></font> methods to get information from the database via models and we also know that this is possible because<font color="#3366ff"> <strong>Active Record::Base</strong></font> provides us the support for the same. </font><font size="1">Some of the normal ones which are most used by developers are <font color="#3366ff"><strong>find(id)</strong></font>,<font color="#3366ff"> <strong>find_all</strong></font> and <font color="#3366ff"><strong>find_first</strong></font>. All these three are the derivatives of <font color="#3366ff"><strong>ActiveRecord::Base::find(*args)</strong></font> and most of the information are handled by these. This method takes many parameters which can be used to create easy operations. The parameters are<font color="#000000">:</font> </font></p>
<ul>
<li><font size="1"><strong><font color="#ff0033">:conditions</font></strong> (for where clause)</font></li>
<li><font size="1"><font color="#ff0000"><strong> <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> rder</strong></font> (determines the order)</font></li>
<li><font size="1"><strong><font color="#ff0000">:group</font></strong> (grouping data)</font></li>
<li><font size="1"><font color="#ff0000"><strong>:limit</strong></font>(specify maximum number of rows to be retrieved)</font></li>
<li><font size="1"><strong><font color="#ff0000"> <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> ffset</font></strong>(number of records to be omitted)</font></li>
<li><font size="1"><font color="#ff0000"><strong>:joins</strong></font>(join multiple tables)</font></li>
<li><font size="1"><strong><font color="#ff0000">:include</font></strong>(for associations with left outer join)</font></li>
<li><font size="1"><strong><font color="#ff0000">:select</font></strong>(to specify the attributes of the output data)</font></li>
<li><font size="1"><strong><font color="#ff0000">:readonly</font></strong>(to execute as reader) </font></li>
</ul>
<p><font size="1">In this post, we shall not look into more details of each of this segment, possibly we can write another post to provide information on these.</font> <font size="1">So most of the queries that are used by Rails developers are any of the above ones. However it becomes an issue to resolve. I shall explain in this post 3 methods which makes developers&#8217; life easy. These finders are: </font></p>
<ol>
<li><font size="1"><strong>Query finder Method (Generalized Finder)</strong> : Query finder is a methodology where the user finds information from the database by use of SQL queries. The developer uses standard SQL Queries in multiple form and executes them using the <font color="#3366ff"><strong>find_by_sql</strong></font> method<font color="#000000">.</font></font></li>
<li><font size="1"><strong>Dynamic finder Method (Specialized Finder)</strong> : This method uses the <strong><font color="#3366ff">method_missing</font> </strong> method of the <font color="#3366ff"><strong>ActiveRecord::Base</strong></font> class to identify and create finder methods which can be declared and used dynamically. However these have to follow some rules.They can be applied only in conjunction with table fields. The four methods used are <font color="#3366ff"><strong>find_by_xxx</strong></font>, <font color="#3366ff"><strong>find_all_by_xxx</strong></font>, <font color="#3366ff"><strong>find_or_create_by_xxx</strong></font> and <font color="#3366ff"><strong>find_or_initialize_by_xxx</strong></font> methods. Dynamic finders also use the<strong> <font color="#3366ff">_and_ </font></strong> keyword to use multiple table fields in the single request.</font></li>
<li><font size="1"><strong>Custom finder Method (Customized Finder):</strong> This method uses writing custom <font color="#3366ff">find*</font> methods which could be either un-conventional method (not as used in above methods) or override the methods declared by the <font color="#3366ff"><strong>ActiveRecord::Base</strong></font>. Custom finders are declared in the Model objects and are generally tagged by<font color="#3366ff"> <strong>self</strong></font> keyword<font color="#000000">.</font></font></li>
</ol>
<p><font size="2"><strong>METHOD #1: Query Finders Method (Generalized Find)</strong></font></p>
<p><font size="1">Many a times the developers are comfortable to use standard SQL queries to write their applications database requests rather than using any of the above <font color="#3366ff">find</font> methods or parameters and they get lost identifying these parameters. Also for complex queries it becomes difficult to use<font color="#000000">.</font></font></p>
<p><font size="1">However <strong><font color="#3366ff">ActiveRecord::Base</font> </strong>again comes to the rescue and gives one another method <font color="#3366ff"><strong>find_by_sql</strong></font> . Using this method the developers can write SQL queries and execute them. The advantage of this system would also be that it could be used for executing stored procedures also. This is advantageous because MySQL 5.0 comes in with Stored Procedures.</font></p>
<p><font size="1">The following are few examples of using <font color="#3366ff">find_by_sql</font> methods</font></p>
<p><em><font size="1"><strong>Retrieving complete information of selected records:</strong></font></em></p>
<p><font size="1">Developers can just key in the sql query and get the information. Example:</font></p>
<p><font size="1"><strong><font color="#004080">@loggedinusers</font></strong></font><font size="1"> = User.find_by_sql(<font color="#2a00ff">&#8220;select * from users where loggedin=true;&#8221;</font>)</font></p>
<p><font size="1">The <strong><font color="#004080">@loggedinusers</font></strong> element shall contain all the logged in users information. </font></p>
<p><font size="1">However if there are inputs required which are unknown then generic find by sql could be written as similar to<font color="#3366ff"> :conditions</font> of  Find method. An example could be as follows:</font><br />
<font size="1"><font color="#004080" size="1"><strong><br />
@loggedinusers</strong> <font color="#000000" size="1">=User.find_by_sql([</font><font color="#2a00ff" size="1">"select * from users where loggedin=? and isActive=?"</font><font color="#000000" size="1">,</font><font color="#004080" size="1"><strong>@isloggedin</strong></font><font color="#000000" size="1">,</font><font color="#004080" size="1"><strong>@isactive</strong></font><font size="1"><font color="#000000">]</font></font></font></font><font size="1"><font color="#004080" size="1"><br />
<font size="1"><u></u></font></font></font></p>
<p><em><font size="1"><font color="#000000" size="1"><font size="1"><strong>Retrieving partial information as required:</strong></font></font></font></em><font size="1"> </font></p>
<p><font size="1">In the above example, we identified that the inputs could be varied and outputs has to be the complete database record. When these operations are done, the advantage is that the objects can be directly mapped to a particular database record. However in this world there are requirements of all types. There are requirements where only particular information has to be populated for these types of inputs we could use.</font></p>
<p align="left"><font size="1"><font color="#004080" size="1"><strong>@loggedinusers</strong> <font color="#000000" size="1">= User.find_by_sql([</font><font color="#2a00ff" size="1">"SELECT username, lastloggedindate, isActive FROM users"</font><font size="1"><font color="#000000">])</font></font></font></font></p>
<p align="left"><font size="1">The <strong><font color="#0000cc">@loggedinusers</font></strong> contains only the information about the username, lastloggedindate and isActive. The following code snippet can be used to get the information:</font></p>
<p align="left"><font size="1"><font color="#000000">          loggedinuser = </font><font color="#004080" size="1">@loggedinusers</font> <font size="1">[</font><font color="#0080ff" size="1">0</font><font size="1"><strong>]</strong><br />
puts loggedinuser.username<br />
puts loggedinuser.lastloggedindate<br />
puts loggedinuser.isActive</font></font></p>
<p><strong><font size="2">METHOD #2: Dynamic Finders Method (Specialized Find )</font></strong></p>
<p><font size="1">Dynamic finders are a method of finding information based on particular database field of a table. If <font color="#3366ff"> <strong>find_by_sql</strong></font> method provides a <font color="#000000">generalized</font> method to query information from the database, by using dynamic finders, you can construct methods to find records based on specific requirements which are input as parameters. </font></p>
<p><font size="1">There are 4 types of dynamic finders which can be used: </font></p>
<ol>
<li><font size="1"><font color="#3366ff">find_by_XXX</font> methods: Finds the first row for a given database field name(s).</font></li>
<li><font size="1"><font color="#3366ff">find_all_by_XXX</font> methods: Finds all the rows that satisfies a given database field name(s). </font></li>
<li><font size="1"><font color="#3366ff">find_or_initialize_by_XXX</font>: Finds a row with a given parameter(s) and if not available, creates a new row and returns back to the user the empty object to be filled.</font></li>
<li><font size="1"><font color="#3366ff">find_or_create_by_XXX</font> methods: Finds a row with particular parameters and if not available, then will be created in the database with the input parameters and then returned.</font></li>
</ol>
<p><em><strong><font size="1">find_by_XXX methods:</font></strong></em></p>
<p><font size="1">This method is used to find any record by a given input parameter. This makes developers&#8217; life easier to find records by any database field. One example would be is to find the record of the person whose username is <font color="#3366ff"><strong>&#8216;heurionconsulting&#8217;</strong></font>. </font></p>
<p><strong><font color="#004080"><font size="1">@user</font><font size="1">= User.find_by_username(<font color="#2a00ff">&#8220;heurionconsulting&#8221;</font>) </font></font></strong></p>
<p><font size="1">The example writes to :</font><br />
<font size="1"><font color="#004080"><strong>@user</strong></font><font color="#000000"> = User.find_by_sql([</font><font color="#2a00ff">"SELECT Top(1) * FROM users WHERE username=?"</font><font color="#000000">,</font><font color="#2a00ff">"heurionconsulting"</font><font size="+0"><font color="#000000">])</font></font></font></p>
<p><font size="1">The advantage of this is the<font color="#3366ff"> find_by_xxx</font> can also be added in conjunction with<font color="#3366ff"> <strong>_and_</strong></font> option if we have to add multiple database fields to create a query. We could modify our earlier example to find the first user who has logged in last and is a purchaser.</font></p>
<p><font color="#004080"><font size="1"><strong>@firstloggedinuser</strong></font><font size="1"><font color="#000000">= User.find_by_loggedindate_and_ispurchaser( </font><font color="#2a00ff">&#8220;05/08/2007&#8243;</font><font color="#000000"> , </font><font color="#2a00ff">&#8220;true&#8221; </font><font color="#000000">)</font></font></font></p>
<p><font color="#000000" size="1">The above example explains to return the first user information who has <font color="#3366ff">loggedindate</font> and is a purchaser. This is equivalent to </font><font size="1"><font color="#004080"><strong><font color="#004080"><br />
@firstloggedinuser</font></strong><font color="#000000"> = User.find_by_sql(</font><font color="#2a00ff">&#8220;SELECT Top(1) * FROM users WHERE loggedindate=&#8217;05/08/2007&#8242; AND ispurchaser=&#8217;true&#8217;; &#8220;</font><font size="+0"><font color="#000000">)</font></font></font></font><font color="#004080"><font size="1"> </font></font></p>
<p><font size="+0"><font color="#000000"><font size="1">Use of keyword <strong><font color="#3366ff">_and_</font> </strong>helps to identify all the database fields which is required to achieve the output.</font><br />
<font size="1">This method is used only to get the first record of the result only.</font></font></font><font color="#004080"><strong><u><font size="1"> </font></u></strong></font></p>
<p><em><font size="+0"><strong><font color="#000000" size="1">find_all_by_XXX methods:</font></strong></font></em></p>
<p><font size="+0"><font color="#000000"><font size="1">This method is similar to the<font color="#3366ff"> find_by_XXX</font> method, but this is used when you want all the records that satisfy the criteria. The<font color="#3366ff"> </font><strong><font color="#3366ff">_and_</font> </strong>criteria could be used for this object as well.</font><br />
<font size="1">An example of  <font color="#3366ff">find_all_by_XXX</font> method could be as follows: </font></font></font></p>
<p align="left"><font size="+0"><font color="#004080"><font size="1"><strong>@loggedinusers</strong></font><font size="1"><font color="#000000">= User.find_all_by_loggedindate_and_ispurchaser(</font><font color="#2a00ff"> &#8220;05/08/2007&#8243; </font><font color="#000000">,</font><font color="#2a00ff"> &#8220;true&#8221;</font><font color="#000000">)</font></font></font></font></p>
<p align="left"><font size="1">This example results in:</font></p>
<p align="left"><font size="1"><font color="#004080"><strong>@loggedinusers</strong></font><font color="#000000"> = User.find_by_sql(</font><font color="#2a00ff">&#8220;SELECT * FROM users WHERE loggedindate= &#8217;05/08/2007&#8242; AND ispurchaser=&#8217;true&#8217;; &#8220;</font><font size="+0"><font color="#000000">)</font></font></font></p>
<p align="left"><em><font size="1"><strong>find_or_initialize_by_XXX methods:</strong></font></em></p>
<p align="left"><font size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><strong>@product</strong><font color="#000000" size="1">= Product.find_or_initialize_by_product_name(</font><font color="#2a00ff" size="1"><strong>&#8220;MUG&#8221;</strong></font><font size="1"><font color="#000000">)</font></font></font></font></font></font></font></font></p>
<p><font size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#a4357a" size="1"><strong>if</strong></font><font color="#004080" size="1"><strong>@product</strong></font><font size="1"><font color="#000000">.product_name.nil?</font><br />
<strong>     redirect_to </strong></font><strong><font color="#ff4040" size="1">:action</font><font size="1">=&gt;</font><font color="#2a00ff" size="1">&#8216;viewproduct&#8217;</font><font size="1">, </font><font color="#ff4040" size="1">:locals</font><font size="1">=&gt;{</font><font color="#ff4040" size="1">:product</font><font size="1"> =&gt; </font><font color="#004080" size="1">@product</font><font size="1">}<br />
</font></strong><font color="#a4357a" size="1"><strong>else</strong></font><font size="1"><br />
<strong>        redirect_to </strong></font><strong><font color="#ff4040" size="1">:action</font><font size="1">=&gt;</font><font color="#2a00ff" size="1">&#8216;newproduct&#8217;</font><font size="1">,</font><font color="#ff4040" size="1">:locals</font><font size="1">=&gt;{</font><font color="#ff4040" size="1">:product</font><font size="1"> =&gt; </font><font color="#004080" size="1">@product</font><font size="1">}</font></strong><font color="#a4357a" size="1"><br />
<strong>end<br />
</strong></font><font color="#a4357a" size="1"><font color="#000000">In the above example, the method shall first find if <strong><font color="#2a00ff" size="1">&#8220;MUG&#8221;</font> </strong>is available in the database. If the product is not available, then the user could create a new product with a new product id and the product name shall be <strong><font color="#2a00ff" size="1">&#8220;MUG&#8221;</font></strong>. So in the newproduct page the user sets only those information whic</font><font color="#000000">h is missing and saves the information to the database.</font></font></font></font></font></font></font></font></p>
<p><em><font size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#004080" size="1"><font color="#a4357a" size="1"><font color="#000000" size="1"><strong>find_or_create_by_XXX methods:</strong></font></font></font></font></font></font></font></font></em></p>
<p><font color="#a4357a" size="1"><font color="#000000" size="1">This method is a complement of the <font color="#3366ff">find_by_intialize_by_XXX</font> where in the initialize case, the user just initializes the object with the input information and gets the object. Whereas in case of <font color="#3366ff"> find_or_create_by_XXX</font> method, the user can provide complete information required to save an object. </font></font><font color="#a4357a" size="1"><font color="#000000" size="1">Modifying the above example we could re-write it as: </font></font></p>
<p align="left"><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font size="1"><strong>@product</strong> </font><font size="1"><font color="#000000">= Product.find_or_create_by_product_name(</font><font color="#2a00ff"><strong>&#8220;MUG&#8221;,<font color="#ff4040"><strong>:create_args</strong></font><font color="#000000"> =&gt; {</font><font color="#ff4040"><strong>:product_name</strong></font><font color="#000000"> =&gt; </font><font color="#2a00ff">&#8216;Mug&#8217;</font><font color="#000000">, </font><font color="#ff4040"><strong>:description</strong></font><font color="#000000">=&gt;</font><font color="#2a00ff">&#8220;New Product&#8221;</font><font color="#000000">, </font><font color="#ff4040"><strong>:availability</strong></font><font color="#000000">=&gt;</font><font color="#a4357a"><strong>true</strong></font><font color="#000000">}</font></strong></font><font color="#000000">)</font></font></font></font></font></font></font></font></font></p>
<p><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#000000" size="1">This method uses a <font color="#3366ff"><strong>:create_args</strong></font> parameter which has to be filled if the object is not found.</font><font color="#000000" size="1"> So if there is no product by name <font color="#3366ff">mug</font>, then create a product and set its information as in the <font color="#3366ff"><strong>:create_args</strong></font> section and return the object.</font></font></font></font></font></font></font></font></p>
<p><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#000000"><font size="2">P.S: All these 4 dynamic finder methods are actually calculated in the <strong><font color="#3366ff">method_missing</font> </strong>method declared in the <font color="#3366ff"><strong>ActiveRecord::Base<font color="#000000">.</font></strong></font></font></font></font></font></font></font></font></font></font></p>
<p><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#000000"><strong><font size="2">METHOD #3: Custom Finders Method (Customized Find)</font></strong></font></font></font></font></font></font></font></font></p>
<p><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#000000" size="1">In the earlier 2 types we have queried the database by: <font color="#3366ff"> a)</font> Providing the complete sql syntax i.e. <font color="#3366ff"><strong>find_by_sql </strong></font>method and <font color="#3366ff">b)</font> By modifying the find method to read the required syntax i.e. <font color="#3366ff"><strong>find_by/all_</strong></font> or <font color="#3366ff"><strong>find_or_initialize/create_by</strong></font> methods. Both of these methods are good and have advantages on their own. But there are many other scenarios where any of these methods cannot be used.</font></font></font></font></font></font></font></font></p>
<p><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#000000" size="1">One classic example would be where you would like to evaluate a scenario before you call the Active record&#8217;s find method. For such scenarios the best approach is to use custom finders.</font></font></font></font></font></font></font></font></p>
<p><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#000000" size="1">Custom finders are nothing but writing find methods within the Model and call the <font color="#3366ff"><strong>ActiveRecord::Base::Find*</strong></font> methods from these methods. Custom finders generally are<font color="#3366ff"><strong> self</strong></font> methods declared in the Model object. </font></font></font></font></font></font></font></font></p>
<p><font color="#a4357a" size="1"><font size="+0"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#004080"><font color="#000000" size="1"><em><strong>#Product.rb &#8212; Model file</strong> </em></font></font></font></font></font></font></font></font></p>
<p align="left"><font color="#000000" size="1"><font color="#a4357a" size="1"><strong>def</strong><font size="1"> <font color="#000000">self.find_product_by_status(status)</font><br />
</font><font color="#a4357a" size="1"><strong>    if</strong></font><font size="1"> <font color="#000000">status !=</font> </font><font color="#ff4040" size="1"><strong> <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> utofshelf<br />
</strong></font><font size="1">            <font color="#000000">        Product.find_by_sql [</font></font><font color="#2a00ff" size="1">"SELECT * FROM product WHERE status=?"</font><font size="1"><font color="#000000">,status]</font><br />
</font><font color="#a4357a" size="1"><strong>    else</strong> </font><font size="1"><br />
<strong>            </strong><font color="#000000">        Product.find_by_sql [</font></font><font color="#2a00ff" size="1">"SELECT * FROM product WHERE status= 'outofshelf' AND availability=false"</font><font size="1"><font color="#000000">]</font><br />
</font><font color="#a4357a" size="1"><strong>    end</strong></font><font size="1"><br />
</font><font color="#a4357a" size="1"><strong>end</strong></font></font></font></p>
<p><font color="#000000" size="1"><font color="#a4357a" size="1"><font color="#000000" size="1">The example above identifies a custom finder where it checks if the status is not out of shelf, then get all the products based on the status and if it is out of shelf, then check the status and also is not available. Also if you see the name of the find method it is an un-conventional name <font color="#3366ff">find _product_by_status</font> instead of  <font color="#3366ff">find_by_status</font>.</font></font></font></p>
<p><font color="#000000" size="1"><font color="#a4357a" size="1"><font color="#000000" size="1">Developers can utilize custom finder to name methods by thier own way or even custom finders can also be used to over write the method names which exists in the <strong><font color="#3366ff">ActiveRecord::Base</font></strong> class. An example can be found as follows:</font></font></font></p>
<p align="left"><font size="1"><font color="#a4357a" size="1"><font color="#a4357a" size="1"><strong><font color="#000000" size="1"><em><strong>#Product.rb &#8212; Model file</strong> </em></font></strong></font></font></font></p>
<p align="left"><font size="1"><font color="#a4357a" size="1"><font color="#a4357a" size="1"><strong>def</strong><font size="1"><font color="#000000"> self.find_by_status(status)</font><br />
<strong>    </strong></font><font color="#a4357a" size="1"><strong>if  </strong></font><font size="1"><font color="#000000">status !=</font><strong> </strong></font><font color="#ff4040" size="1"><strong> <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> utofshelf<br />
</strong></font><font size="1"><font color="#000000">        Product.find_by_sql [</font></font><font color="#2a00ff" size="1">"SELECT * FROM product WHERE status=? AND availability=false"</font><font color="#000000" size="1">]<br />
</font><font color="#a4357a" size="1"><strong>    else</strong><br />
</font><font color="#a4357a" size="1"><strong>        super</strong><br />
</font><font color="#a4357a" size="1"><strong>    end</strong><br />
</font><font color="#a4357a" size="1"><strong>end</strong></font></font></font></font></p>
<p><font size="1"><font color="#a4357a" size="1"><font color="#a4357a" size="1"><font size="1"><font color="#000000">In this example, you see that the custom finder has the same name as a <font color="#3366ff"><strong>find_by_status</strong></font> method which is a standard <font color="#3366ff"><strong>ActiveRecord::Base </strong></font>method and has been overwritten by the find method in the Model file. In the example, we have also dealt in re-directing the custom finder method back to its parent method by using the</font> <font color="#3366ff"><strong>super</strong></font>  <font color="#000000">keyword.</font> </font></font></font></font></p>
<p><font size="1"><font color="#a4357a" size="1"><font color="#a4357a" size="1"><font size="1"><font color="#000000" size="2"><strong>To Summarize</strong></font></font></font></font></font></p>
<p><font size="1"><font color="#a4357a" size="1"><font color="#a4357a" size="1"><font size="1"><font color="#000000" size="1">In this post we have explained various types of find methods.</font></font></font></font></font></p>
<ol>
<li><font size="1">We have explained in brief about<strong> <font color="#3366ff">Find</font></strong> method and its parameters. However going in depth of this method is out of the scope of this article.</font></li>
<li><font size="1">We have dealt with how to use <font color="#3366ff"><strong>find_by_sql</strong></font> method to query complete and partial record information (with and with out multiple inputs).</font></li>
<li><font size="1">We have come across how to use dynamic finders by just the names of the database fields with<font color="#3366ff"> </font><strong><font color="#3366ff">_and_</font> </strong>combination to find single, all, find &amp; initialize and find &amp; create methods by using <font color="#3366ff"><strong>find_by/all_</strong></font> and <font color="#3366ff"><strong>find_or_create/initialize_by</strong></font> methods.</font></li>
<li><font size="1">We have also explained how to use custom finders which can be used either as a wrapper on general find methods or override the <font color="#3366ff"><strong>ActiveRecord::Base</strong></font>&#8216;s find methods.</font></li>
</ol>
<p><font size="1">We hope these information is useful for the reader and are open to any comments.</font></p>
<p><strong>&#8211; Heurion Consulting Information Release</strong></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/heurionconsulting.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/heurionconsulting.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/heurionconsulting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/heurionconsulting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/heurionconsulting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/heurionconsulting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/heurionconsulting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/heurionconsulting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/heurionconsulting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/heurionconsulting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/heurionconsulting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/heurionconsulting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/heurionconsulting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/heurionconsulting.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/heurionconsulting.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/heurionconsulting.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=6&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://heurionconsulting.wordpress.com/2007/05/24/using-find-methods-in-railsactiverecord/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/023d73a0417c34c19195a4dc9b55d9a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">heurionconsulting</media:title>
		</media:content>
	</item>
		<item>
		<title>In Place Editing with RJS</title>
		<link>http://heurionconsulting.wordpress.com/2007/05/23/in-place-editing-with-rjs/</link>
		<comments>http://heurionconsulting.wordpress.com/2007/05/23/in-place-editing-with-rjs/#comments</comments>
		<pubDate>Wed, 23 May 2007 06:17:09 +0000</pubDate>
		<dc:creator>heurionconsulting</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://heurionconsulting.wordpress.com/2007/05/23/in-place-editing-with-rjs/</guid>
		<description><![CDATA[In this post, we shall provide the snippets and explanation of how to use Inplace editor and RJS to update multiple elements in your page. The Problem In-Place Editing is a wonderful AJAX based tool that helps users to provide a text field to update and render information that suits the Web2.0 building. Users can [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=5&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><font size="2"> In this post, we shall provide the snippets and explanation of how to use Inplace editor and RJS to update multiple elements in your page.</font></p>
<p><font size="2"><strong>The Problem</strong> </font></p>
<p><font size="2"><strong>In-Place Editing </strong>is a wonderful AJAX based tool that helps users to provide a text field to update and render information that suits the Web2.0 building. Users can use In-Place Editor to either directly connect to a database record for query and updating or can make use of set_XXX_YYY methods to update yourself.</font></p>
<p><font size="2"> The main issue of In-Place Editor is that it has direct links to update the database record or possibly you can validate information. However there is no way directly if you have multiple elements of the page to be updated according to the information provided in the In-Place Editor.</font></p>
<p><font size="2"><strong>The Solution </strong></font></p>
<p><font size="2">The Solution to update multiple elements after the In-Place editor updates itself can be achieved using <strong>RJS (Ruby Java Script)</strong>. You can first use the In-Place Editor to take inputs from the user and then once you submit information to the database, you can call an RJS to update the In-Place editor and any other elements in the page. </font></p>
<p><font size="2"><strong>The Case Study</strong></font></p>
<p><font size="2">In this case study we shall assume that the user knows what RJS and In-Place editing is all about, but doesn&#8217;t know how to link both. </font></p>
<p><font size="2">The Case Study is based on an issue which was identified by us few days ago, where we were using In-Place Editor to take inputs about the width and height of an image and store it in the database. However we also had to update our preview image to show this width and height. </font></p>
<p><font size="2">The Case study uses a table by name <font color="#3366ff">images</font> which holds information such as id, name, width and height and we assume these images are stored with-in public/images/sampleimages folder with the id as image name. (In the example you shall see us referring to this link.) </font></p>
<p><font size="2"><strong>The Method</strong></font></p>
<p><font size="2">Lets start this method from the base and then come towards the UI. </font></p>
<p><font size="2">First is the Database:<br />
We create a Table by name images with id, name, width and height information.</font></p>
<p><font size="2">Once the database is ready, we start the coding part. As it is known, we directly use the table information, not doing any validation part . So lets keep it simple and do anything at the model end. </font></p>
<p><font size="2">Coming to the controller, we have 2 parts to do: </font></p>
<ol>
<li><font size="2">Create a method which shall launch the rhtml. Lets do all our work to pass to the <font color="#3366ff">viewimage.rhtml<font color="#000000">.</font></font></font></li>
<li><font size="2">Update the image information when user clicks on Ok in the In-Place Editor.</font></li>
</ol>
<p><font size="2">So our Controller should look as follows:</font></p>
<p><font size="2"><strong><font color="#a4357a" size="2">class<font size="2"> CategoryadminController &lt; ApplicationController</font></font><br />
<font color="#a4357a" size="2"><br />
def</font></strong><font size="2"> viewimage<strong><font color="#004080" size="2"><br />
@image</font></strong><font size="2"> = Image.find(params[</font><strong><font color="#ff4040" size="2">:id</font></strong><font size="2">])<strong><font color="#a4357a" size="2"> </font></strong></font><strong><font color="#a4357a" size="2"><br />
end</font></strong></font></font></p>
<p><font size="2"><font size="2"><strong><font color="#a4357a">end</font></strong></font><font size="2"><strong><font color="#a4357a"> </font></strong></font></font></p>
<p><font size="2"><font size="2"><font color="#a4357a"><font size="2"><font color="#000000">All we do here is to get the selected image from the database and put it into the </font><strong><font color="#004080" size="2">@image</font></strong><font color="#000000"> variable. Once our method is ready we are set to write the <font color="#3366ff">viewimage.rhtml<font color="#000000">.</font></font> In the <font color="#3366ff">viewimage.rhtml</font> file, we have the following elements:</font></font></font></font></font></p>
<ol>
<li><font size="2">We have an area where we show the name. This can be a simple <font color="#3366ff">in_place_editor_field</font> which you can get updated directly with the database (out of our scope).</font></li>
<li><font size="2">We have an area where user can see the preview image with the width and height of the image.</font></li>
<li><font size="2">We have an area where we show width and height of the image. This uses <font color="#3366ff">in_place_editor</font> and update on these fields should affect the preview image&#8217;s width and height</font></li>
</ol>
<p><font size="2">The code for all these three should look as follows:</font></p>
<p><font size="2"><font color="#3f7f7f" size="2">&lt;div </font><font color="#7f007f" size="2">id</font><font size="2">=</font><font color="#2a00ff" size="2">&#8220;image_info&#8221;</font><font color="#3f7f7f" size="2">&gt;</font></font></p>
<p><font size="2"><font color="#3f7f7f" size="2">&lt;span <font color="#7f007f" size="2">id</font><font size="2">=</font><font color="#2a00ff" size="2">&#8220;name&#8221;</font><font color="#3f7f7f" size="2"> &gt;</font><font size="2">&lt;%= </font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.name %&gt;</font><font color="#3f7f7f" size="2">&lt;/span&gt;</font><font size="2"> </font><font color="#3f7f7f" size="2">&lt;br /&gt;</font></font></font></p>
<p><font size="2"><font color="#3f7f7f" size="2">&lt;span <font color="#7f007f" size="2">id</font><font size="2">=</font><font color="#2a00ff" size="2">&#8220;width&#8221;</font><font color="#3f7f7f" size="2"> &gt;</font><font size="2">&lt;%= </font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.width %&gt;</font><font color="#3f7f7f" size="2">&lt;/span&gt;&lt;br /&gt;</font></font><br />
<font size="2">&lt;%= in_place_editor </font><font color="#2a00ff" size="2">&#8220;width&#8221;</font><font size="2">, </font><font size="2">{</font><strong><font color="#ff4040" size="2">:url</font></strong><font size="2"> =&gt;url_for </font><strong><font color="#ff4040" size="2">:action</font></strong><font size="2">=&gt;</font><font color="#2a00ff" size="2">&#8220;set_image_width&#8221; </font><font size="2">, </font><strong><font color="#ff4040" size="2">:id</font></strong><font size="2">=&gt;</font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.id) , </font><strong><font color="#ff4040" size="2">:script</font></strong><font size="2">=&gt;</font><strong><font color="#a4357a" size="2">true </font></strong><font size="2">} %&gt;</font><font size="2"> </font><font size="2"><font color="#3f7f7f" size="2">&lt;span <font color="#7f007f" size="2">id</font><font size="2">=</font><font color="#2a00ff" size="2">&#8220;height&#8221;</font><font color="#3f7f7f" size="2"> &gt;</font><font size="2">&lt;%= </font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.height %&gt;</font><font color="#3f7f7f" size="2">&lt;/span&gt;&lt;br /&gt;</font></font><br />
<font size="2">&lt;%= in_place_editor </font><font color="#2a00ff" size="2">&#8220;height&#8221;</font><font size="2">, </font><font size="2">{</font><strong><font color="#ff4040" size="2">:url</font></strong><font size="2"> =&gt;url_for (</font><strong><font color="#ff4040" size="2">:action </font></strong><font size="2">=&gt; </font><font color="#2a00ff" size="2">&#8220;set_image_height&#8221; </font><font size="2">, </font><strong><font color="#ff4040" size="2">:id</font></strong><font size="2">=&gt;</font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.id) , </font><strong><font color="#ff4040" size="2">:script</font></strong><font size="2">=&gt;</font><strong><font color="#a4357a" size="2">true</font></strong><font size="2">} %&gt;</font></font></font></p>
<p align="left"><font size="2"><font size="2"><font size="2"><font color="#3f7f7f" size="2">&lt;/div&gt;</font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font color="#3f7f7f" size="2">&lt;div <font color="#7f007f" size="2">id</font><font size="2">=</font><font color="#2a00ff" size="2">&#8220;image_preview&#8221;</font><font color="#3f7f7f" size="2">&gt;</font></font><br />
<font size="2">&lt;%= image_tag </font><font color="#2a00ff" size="2">&#8220;/images/sampleimages/#{@image.id}.jpg&#8221;</font><font size="2">, </font><font size="2">{ <strong><font color="#ff4040" size="2">:width</font></strong><font size="2">=&gt;</font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.width, </font><strong><font color="#ff4040" size="2">height </font></strong><font size="2">=&gt; </font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.height } %&gt;</font></font><font color="#3f7f7f" size="2"><br />
&lt;/div&gt;</font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font color="#3f7f7f" size="2"><font size="2"><font color="#000000">If you see the in place editor you will see that on click of the ok button you have to raise </font><font color="#3f7f7f" size="2"><font color="#2a00ff" size="2">set_image_width</font> </font><font color="#000000" size="2">method in the controller and also you see</font><font color="#3f7f7f" size="2"> <font size="2"><strong><font color="#ff4040">:script</font></strong><font size="+0">=&gt;</font><strong><font color="#a4357a">true</font></strong></font> </font><font color="#000000" size="2">which means that once the method is called in the controller its next step is to call the RJS.</font></font></font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font color="#3f7f7f" size="2"><font size="2"><font color="#000000" size="2">So now let us update our controller code with these set_XXX_YYY methods. Our controller should now look like:</font></font></font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font size="2"><font color="#a4357a" size="2"><font size="2"><font color="#a4357a" size="2"><strong>d</strong></font></font><font size="2"><font color="#a4357a" size="2"><strong>ef</strong></font><font color="#000000"> set_image_width<br />
image = Image.find(params[</font><font color="#ff4040" size="2"><strong>:id</strong></font><font color="#000000">])<br />
image.width = params[</font><font color="#ff4040" size="2"><strong>:value</strong></font><font color="#000000">]<br />
image.save<br />
</font><font color="#004080" size="2"><strong> @image</strong></font><font color="#000000"> = image</font><font color="#a4357a" size="2"><br />
<strong>end</strong></font></font></font></font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font size="2"><font color="#a4357a" size="2"><font size="2"><font size="2"><font color="#a4357a"><font size="2"><font color="#a4357a" size="2"><font size="2"><font color="#a4357a" size="2"><strong>d</strong></font></font><font size="2"><font color="#a4357a" size="2"><strong>ef</strong></font><font color="#000000"><strong>  </strong>set_image_height<br />
image = Image.find(params[</font><font color="#ff4040" size="2"><strong>:id</strong></font><font color="#000000">])<br />
image.height = params[</font><font color="#ff4040" size="2"><strong>:value</strong></font><font color="#000000">]<br />
image.save<br />
</font><font color="#004080" size="2"><strong> @image</strong></font><font color="#000000"> = image</font><font color="#a4357a" size="2"><br />
<strong>end</strong></font></font></font></font></font></font></font></font></font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font size="2"><font color="#a4357a" size="2"><font size="2"><font size="2"><font color="#a4357a"><font size="2"><font size="2"><font color="#000000">In the above code what we see is first get the image object, get the value of the <font color="#3366ff">in_place_editor</font> and save the information to the database. Once this work is completed, our last piece is to update the RJS. The setting </font><font size="2"><strong><font color="#ff4040">:script</font></strong><font color="#000000" size="+0">=&gt;</font><strong><font color="#a4357a">true </font></strong></font><font color="#000000">in the <font color="#3366ff">viewimage.rhtml</font> and </font><font color="#004080" size="2"><strong>@image</strong><font color="#000000" size="2"> = image</font><strong> </strong></font><font color="#000000">in the controller should help us to run the RJS file to replace the information.<font size="2"> </font></font></font></font></font></font></font></font></font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font size="2"><font color="#a4357a" size="2"><font size="2"><font size="2"><font color="#a4357a"><font size="2"><font size="2"><font color="#000000"><font size="2">First of all you have to create an RJS file which is of the same name as the controller method which is being used. In this case it should be <font color="#3366ff">set_image_width.rjs</font> and <font color="#3366ff">set_image_height.rjs</font>. Once you create these files, all you have to do is query the image object and reload the <font color="#3366ff">div</font> so that new information is reflected in your object.<br />
</font></font></font></font></font></font></font></font></font><font size="2"> <font size="2"><font color="#000000" size="2"><font size="2"><font size="2"><font color="#000000"><font size="2"><strong>#set_image_width.rjs</strong></font></font></font></font></font></font><font size="2"><font color="#a4357a" size="2"><font size="2"><font size="2"><font color="#000000"><font size="2"><font size="2"><br />
page[</font><strong><font color="#ff4040" size="2">:width</font></strong><font size="2">].replace_html( </font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.width)</font><font size="2"><br />
page[<strong><font color="#ff4040" size="2">:image_preview</font></strong><font size="2">].replace_html(image_tag( </font><font color="#2a00ff" size="2">&#8220;/images/sampleimages/#{@image.id}.jpg&#8221;</font><font size="2">, {</font><strong><font color="#ff4040" size="2">:width</font></strong><font size="2">=&gt;</font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.width, </font><strong><font color="#ff4040" size="2">:height</font></strong><font size="2">=&gt;</font><strong><font color="#004080" size="2">@image</font></strong><font size="2">.height}))</font><font size="2"> </font></font></font></font></font></font></font></font></font></font></font></font></p>
<p><font size="2"><font size="2"><font size="2"><font size="2"><font size="2"><font color="#a4357a" size="2"><font size="2"><font size="2"><font color="#000000"><font size="2"><font size="2">What we do in the RJS file is: </font></font></font></font></font></font></font></font></font></font></font></p>
<ol>
<li><font size="2"><font size="2">Replace the In-place Editor span Id with the new modified change &#8212; In-place editor by default doesn&#8217;t do it.</font></font></li>
<li><font size="2"><font size="2">Replace the Preview-image section with the latest width and height information. Sometimes you could just call reload method on the <font color="#3366ff">div</font>, but we don&#8217;t see it working consistently<strong>.</strong></font></font></li>
</ol>
<p><font size="2"><font size="2"><strong>The Summary</strong> </font></font></p>
<p><font size="2"><font size="2">Before we end this session, let us summarize the following:</font></font></p>
<ol>
<li><font size="2"><font size="2">We used <font color="#3366ff">In-Place Editor</font> to provide text editing.</font></font></li>
<li><font size="2"><font size="2">We used <font color="#3366ff">set_image_xxx</font> methods to validate and save image objects.</font></font></li>
<li><font size="2"><font size="2">We used <font color="#3366ff">:script =&gt; true</font> to make the <font color="#3366ff">in_place_editor</font> to call the RJS file.</font></font></li>
<li><font size="2"><font size="2">We created an <font color="#3366ff">set_image_xxx.rjs</font> file where we first replaced the width/height span information with latest image width/height and also set the Preview image&#8217;s width and height parameter to be as what is available in the corresponding image object.</font></font></li>
</ol>
<p><font size="2"><strong>&#8211; Heurion Consulting Information Release</strong></font></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/heurionconsulting.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/heurionconsulting.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/heurionconsulting.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/heurionconsulting.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/heurionconsulting.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/heurionconsulting.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/heurionconsulting.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/heurionconsulting.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/heurionconsulting.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/heurionconsulting.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/heurionconsulting.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/heurionconsulting.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/heurionconsulting.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/heurionconsulting.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/heurionconsulting.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/heurionconsulting.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=5&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://heurionconsulting.wordpress.com/2007/05/23/in-place-editing-with-rjs/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/023d73a0417c34c19195a4dc9b55d9a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">heurionconsulting</media:title>
		</media:content>
	</item>
		<item>
		<title>Thought Works India&#8217;s Master Class Series Seminar</title>
		<link>http://heurionconsulting.wordpress.com/2007/05/22/thought-works-indias-master-class-series-seminar/</link>
		<comments>http://heurionconsulting.wordpress.com/2007/05/22/thought-works-indias-master-class-series-seminar/#comments</comments>
		<pubDate>Tue, 22 May 2007 09:24:23 +0000</pubDate>
		<dc:creator>heurionconsulting</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Web 2.0]]></category>

		<guid isPermaLink="false">http://heurionconsulting.wordpress.com/2007/05/22/thought-works-indias-master-class-series-seminar/</guid>
		<description><![CDATA[In this post, we shall take you through my understanding of how and what went on in the Thought Works’ Master Class Series Seminar held on May 19, 2007 @ Hotel Royal Orchid , Bangalore, India. Disclaimer: This post is made only to point out few areas of technology information which might be interested for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=4&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In this post, we shall take you through my understanding of how and what went on in the <strong>Thought Works’ Master Class Series Seminar</strong> held on <strong>May 19, 2007 @ Hotel Royal Orchid , Bangalore, India</strong>.</p>
<p><font size="1"><strong>Disclaimer:</strong> This post is made only to point out few areas of technology information which might be interested for different kinds of people. And those who would want to benefit from this can do so. This Post is our organization’s evaluation of the seminar and holds no other information or criteria in hosting this. Heurion Consulting has <strong>NOT</strong> been paid by any person to write this post.</font></p>
<p><strong>Master Class Series Seminars</strong> are a new class of technology seminars that Thought Works have started in Bangalore India, This is their second year. Unfortunately I didn’t attend the first one.</p>
<p>The Programme was divided into 3 technology discussions each of them were in different segments of software life cycle. I shall not concentrate on the initial speech or the last vote of thanks because my concentration is on the technology discussion and how it will help us. Also as we know Thought works is known for their agile development practices, they are releasing new software for project management. The technologies discussed were as follows</p>
<ol>
<li>Database Refactoring</li>
<li>Evolutionary testing</li>
<li>Domain Specific Languages</li>
</ol>
<p><span id="more-4"></span></p>
<p><strong>Database Refactoring</strong> was presented by Mr. Pramod Sadalage and identifies use of evolutionary development methodologies such as Extreme Programming, Feature Driven Development to make it clear that developers and DBA need to work together in the same manner to be productive as a team. He presented information and material from his book on &#8220;Refactoring Databases: Evolutionary Database Design&#8221; on how to go about doing evolutionary database development and talked about techniques of database Refactoring, evolutionary data modeling, database testing, configuration Management of database artifacts and developer sandboxes. In his presentation, what we understood was more similar to how the Ruby on Rails technology looks at the Database to be part of the developer frame work. However we see that in his presentation he concentrated that even other technologies could adopt these principles. In his presentation we could list the following points of action.</p>
<ol>
<li>He explains the technique of Developer and DBA to be working together making a pair (This exposes to a concept called as Pair Programming and guys trust me, it works, I have done it some time back). He identified that DBAs should also be part of the cycle where the database elements are being decided. He pointed out that DBA == ROLE != Person</li>
<li>He identified the need for the Database Scripts to be in the source control (similar to our DB/migrate scripts in the code. If some one is not using the Latest Rails framework, they wouldn&#8217;t know what is db/migrate scripts)</li>
<li>He identifies by use of scripts it becomes easy for any person joining an organization to come to speed by automating scripts to create databases by use of ants, subversions, cruise control, etc</li>
<li>He identified how to break all the DB migrations into delta scripts for easy management of the scripts. He demonstrated by naming the scripts with XXX_NAMEOFSCRIPT.sql where XXX refers to the version of the database and these scripts would contain 2 sections one for a Go and other for Undo action (very similar to our Migration script i.e. naming with 002_CREATE_TABLE and having a self.up and self.down sections in the migration files)</li>
<li>He demonstrated these functions with examples and used svn and cruisecontrol for version controlling. We were able to appreciate cruise control&#8217;s usage in build creation. We understand that cruise control is used for creating versions of build and whenever we do a checkin to the svn, cruise control does a build and checks if the code is up to date. I think we need to try this out and see if this brings us advantage to us. For Ruby on rails, there is cruisecontrol.rb</li>
</ol>
<p>Pramod also has a website <a href="www.databaserefactoring.com">www.databaserefactoring.com</a> where he has identified many areas and methods of Refactoring.</p>
<p>For the question that I asked him that RoR already does all these things, so what is new for us? I understand that what we do is migrating data from ruby code and not sql scripts and for thousands of database entries; running migration from the code is costly? I don’t know how far it is true, but since it comes from the horse’s mouth, let me accept it until I find some one telling me that this is not true.</p>
<p><!--more--></p>
<p><strong>Evolutionary Testing</strong> was presented by Mr. Vivek Prahlad who is also one of the designers of the Frankenstein tool. In his presentation he pointed out the usage of their testing tool called <strong>Selenium</strong> which can do functional testing and UI testing. Selenium has the following features.</p>
<ol>
<li>It is an Open source tool, so we can write extensions to the same to support our testing needs. They expose some APIs</li>
<li>It has cross browser support. Selenium, comes with plug-ins, which are added to each of the browsers, so with firefox, they have a plugin at tools à selenium and this plugin can be used to record information of testing and do the testing.</li>
<li>It can test minimal Ajax implementation (but not complete Web 2.0 aspects. Well what features they support and they don’t support, I feel there are many, however what I felt was that this does not support is the Scriptaculous effects. If I have some time, then I shall find out more information on the same).</li>
<li>It supports many languages including Ruby.</li>
<li>Mainly for Functional Testing, but also supports UI testing.</li>
</ol>
<p><!--more--></p>
<p><strong>Selenium</strong> has some features like Recording which helps to record actions on tests and run them on different browsers, These recordings can also be converted to code and write test scripts. The advantage with Selenium is that we can write code in Ruby on Rails. Selenium also supports DRY, use of methods, parameterization and also is object oriented. During the presentations he took through 3 testing patterns and explained how Selenium can support these operations. The patterns were</p>
<ol>
<li>Screen flow pattern</li>
<li>Data Template Pattern</li>
<li>Work flow pattern</li>
</ol>
<p>P.S: When I spoke to Sidu one of the thought worker, he said Frankenstein tool also has Ruby Drivers. I think its worth checking it out.</p>
<p><!--more--></p>
<p><strong>Domain Specific Language</strong> section was presented by Mr. Srihari who identified a need of writing specific interpreters / rule engines for particular domains. He picked this from one of their Ruby on Rails projects where they wrote an interpreter and a parser which converts user&#8217;s English language to be interpreted by use of a parser and Rails project. This project used RACC (Ruby&#8217;s port of YACC) and for lexical Scanning they used Ruby&#8217;s string scan. However this presentation didn&#8217;t give us much information of the exact need and how their approach was. But it was informative to get an understanding that if we have to provide a language for the users to key in rules, then the best solution approach would be to use a Domain Specific Language which is understood by the users.</p>
<p>P.S: I didn&#8217;t appreciate this presentation until I found that there is a book written for this the link is as follows <a href="http://www.pragmaticprogrammer.com/titles/tpantlr/index.html">http://www.pragmaticprogrammer.com/titles/tpantlr/index.html</a> . Now I appreciate what he was trying to tell us.</p>
<p>P.P.S: Sidu says “You may also want to mention that everyone&#8217;s favorite Rails Migrations are a classic example of a DSL.” Thanks for the information Sidu.</p>
<p>Other to these presentations, I was in a discussion with one of the Thoughtworkers regarding their Project Management Product called <strong>MINGL</strong>E. This is a project management tool for agile practices and helps in handling iterations and planning stories. This tool has the following features.</p>
<ol>
<li>Manage Agile projects</li>
<li>Configurable to any industry</li>
<li>Easy management</li>
<li>Good UI</li>
</ol>
<p>The beauty of this tool is that it is developed using Ruby on Rails and has lots of AJAX stuff in it. I got Mr. Kedar (a Thought worker) to give me a demo and it looked good. I understand the BETA release is some time in next 2 months and we can download and test. Also for a &lt; 5 member team, it’s free. You can find more details of Mingle @ <a href="http://studios.thoughtworks.com/mingle-project-intelligence">http://studios.thoughtworks.com/mingle-project-intelligence</a></p>
<p>Hope these are informative. We are planning to test CruiseControl.rb, Frankestine and Selenium and try and integrate with our systems. Once done, we shall put a post on this as well.</p>
<p>So till then&#8230;.do the Ruby on the chuk book chuk book&#8230;..(The Rails&#8230;.)</p>
<p><strong>- Heurion Consulting Information Release</strong></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/heurionconsulting.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/heurionconsulting.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/heurionconsulting.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/heurionconsulting.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/heurionconsulting.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/heurionconsulting.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/heurionconsulting.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/heurionconsulting.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/heurionconsulting.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/heurionconsulting.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/heurionconsulting.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/heurionconsulting.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/heurionconsulting.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/heurionconsulting.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/heurionconsulting.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/heurionconsulting.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=heurionconsulting.wordpress.com&amp;blog=1075216&amp;post=4&amp;subd=heurionconsulting&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://heurionconsulting.wordpress.com/2007/05/22/thought-works-indias-master-class-series-seminar/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/023d73a0417c34c19195a4dc9b55d9a1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">heurionconsulting</media:title>
		</media:content>
	</item>
	</channel>
</rss>
