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

<channel>
	<title>Max Ivak Personal Site</title>
	<atom:link href="http://maxivak.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxivak.com</link>
	<description>Entrepreneur, Business Angel, Software Architect. Startups. SaaS, Business Intelligence</description>
	<lastBuildDate>Wed, 11 Apr 2012 15:20:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>ASP.NET MVC file uploads using Valums Ajax Uploader plugin</title>
		<link>http://maxivak.com/asp-net-mvc-file-uploads-using-valums-plugin/</link>
		<comments>http://maxivak.com/asp-net-mvc-file-uploads-using-valums-plugin/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 20:57:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net mvc]]></category>
		<category><![CDATA[upload file]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=837</guid>
		<description><![CDATA[File uploading is one of the common tasks in web development. One of the best scripts for file uploading is valums/file-upoader. &#160; Valums Ajax Upload Plugin Overview This plugin uses XHR for uploading multiple files with progress-bar in FF3.6+, Safari4+, Chrome and falls back to hidden iframe based upload in other browsers, providing good user [...]]]></description>
			<content:encoded><![CDATA[<p>File uploading is one of the common tasks in web development.</p>
<p>One of the best scripts for file uploading is <a href="https://github.com/valums/file-uploader"><strong>valums/file-upoader</strong></a>.</p>
<p>&nbsp;</p>
<p><span id="more-837"></span></p>
<h2>Valums Ajax Upload Plugin Overview</h2>
<p>This plugin uses XHR for uploading multiple files with progress-bar in FF3.6+, Safari4+, Chrome and falls back to hidden iframe based upload in other browsers, providing good user experience everywhere.</p>
<h3>Features</h3>
<ul>
<li>multiple file select, progress-bar in FF, Chrome, Safari</li>
<li>drag-and-drop file select in FF, Chrome</li>
<li>uploads are cancellable</li>
<li>no external dependencies</li>
<li>doesn&#8217;t use Flash</li>
<li>fully working with https</li>
<li>keyboard support in FF, Chrome, Safari</li>
<li>tested in IE7,8; Firefox 3,3.6,4; Safari4,5; Chrome; Opera10.60;</li>
</ul>
<p>&nbsp;</p>
<h2>Setup File Uploader plugin on ASP.NET MVC site</h2>
<p>Here is step-by-step instructions how to setup file-uploader script on your page on ASP.NET MVC site.</p>
<p>- Download the script from <a href="https://github.com/valums/file-uploader">github</a>.</p>
<p>- Save files to the root of your project (or any folder you want). You need these three files:</p>
<p>/js/fileuploader.js</p>
<p>/css/fileuploader.css</p>
<p>/img/loading.gif</p>
<p>&nbsp;</p>
<p>- Put the script on your page (view):<br />
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;"><span class="sy0">&lt;</span>link href<span class="sy0">=</span><span class="st0">&quot;@Url.Content(&quot;</span>~<span class="sy0">/</span>css<span class="sy0">/</span>fileuploader.<span class="me1">css</span><span class="st0">&quot;)&quot;</span> rel<span class="sy0">=</span><span class="st0">&quot;stylesheet&quot;</span> type<span class="sy0">=</span><span class="st0">&quot;text/css&quot;</span> <span class="sy0">/&gt;</span>
<span class="sy0">&lt;</span>script src<span class="sy0">=</span><span class="st0">&quot;@Url.Content(&quot;</span>~<span class="sy0">/</span>js<span class="sy0">/</span>fileuploader.<span class="me1">js</span><span class="st0">&quot;)&quot;</span> type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;&lt;/</span>script<span class="sy0">&gt;</span>
&nbsp;
<span class="sy0">&lt;</span>div id<span class="sy0">=</span><span class="st0">&quot;file-uploader&quot;</span><span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>noscript<span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>p<span class="sy0">&gt;</span>Please enable JavaScript to use file uploader.<span class="sy0">&lt;/</span>p<span class="sy0">&gt;</span>
<span class="sy0">&lt;/</span>noscript<span class="sy0">&gt;</span>
<span class="sy0">&lt;/</span>div<span class="sy0">&gt;</span>
&nbsp;
<span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span>
var uploader <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> qq.<span class="me1">FileUploader</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
element<span class="sy0">:</span> document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'file-uploader'</span><span class="br0">&#41;</span>,
action<span class="sy0">:</span> <span class="st0">'@Url.Action(&quot;upload&quot;)'</span> <span class="co1">// put here a path to your page to handle uploading</span>
<span class="co1">//,allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'] // user this if you want to upload only pictures</span>
sizeLimit<span class="sy0">:</span> <span class="nu0">4000000</span>, <span class="co1">// max size, about 4MB</span>
minSizeLimit<span class="sy0">:</span> <span class="nu0">0</span>, <span class="co1">// min size</span>
&nbsp;
 <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div></p>
<p>&nbsp;</p>
<p>- Create an ASP.NET MVCÂ  controller which will handle the file uploading and save the file to some location on the server:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw4">class</span> HomeController <span class="sy0">:</span> Controller
<span class="br0">&#123;</span>
..
<span class="br0">&#91;</span>HttpPost<span class="br0">&#93;</span>
<span class="kw1">public</span> ActionResult Upload<span class="br0">&#40;</span>HttpPostedFileBase qqfile<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
<span class="kw1">if</span> <span class="br0">&#40;</span>qqfile <span class="sy0">!=</span> <span class="kw1">null</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
<span class="co1">// this works for IE</span>
var filename <span class="sy0">=</span> Path.<span class="me1">Combine</span><span class="br0">&#40;</span>Server.<span class="me1">MapPath</span><span class="br0">&#40;</span><span class="st0">&quot;~/app_data&quot;</span><span class="br0">&#41;</span>, Path.<span class="me1">GetFileName</span><span class="br0">&#40;</span>qqfile.<span class="me1">FileName</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
qqfile.<span class="me1">SaveAs</span><span class="br0">&#40;</span>filename<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">return</span> Json<span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> <span class="br0">&#123;</span> success <span class="sy0">=</span> <span class="kw1">true</span> <span class="br0">&#125;</span>, <span class="st0">&quot;text/html&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="kw1">else</span>
<span class="br0">&#123;</span>
<span class="co1">// this works for Firefox, Chrome</span>
var filename <span class="sy0">=</span> Request<span class="br0">&#91;</span><span class="st0">&quot;qqfile&quot;</span><span class="br0">&#93;</span><span class="sy0">;</span>
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span><span class="kw4">string</span>.<span class="me1">IsNullOrEmpty</span><span class="br0">&#40;</span>filename<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
filename <span class="sy0">=</span> Path.<span class="me1">Combine</span><span class="br0">&#40;</span>Server.<span class="me1">MapPath</span><span class="br0">&#40;</span><span class="st0">&quot;~/app_data&quot;</span><span class="br0">&#41;</span>, Path.<span class="me1">GetFileName</span><span class="br0">&#40;</span>filename<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw1">using</span> <span class="br0">&#40;</span>var output <span class="sy0">=</span> <span class="kw5">System.<span class="me1">IO</span></span>.<span class="me1">File</span>.<span class="me1">Create</span><span class="br0">&#40;</span>filename<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span class="br0">&#123;</span>
Request.<span class="me1">InputStream</span>.<span class="me1">CopyTo</span><span class="br0">&#40;</span>output<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="kw1">return</span> Json<span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> <span class="br0">&#123;</span> success <span class="sy0">=</span> <span class="kw1">true</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="br0">&#125;</span>
<span class="kw1">return</span> Json<span class="br0">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> <span class="br0">&#123;</span> success <span class="sy0">=</span> <span class="kw1">false</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>Note that the controller action must return JsonResult.</p>
<p>Also note that there are two different ways to handle uploads from IE and other (Firefox/Chrome) browsers. The issue here that IE uploads uses multipart-mime while other browsers use Octet-Stream. And Valums Ajax plugin sends anÂ <code>application/octet-stream</code>Â instead ofÂ <code>multipart/form-data </code>which is what the default model binder can work with.</p>
<p>&nbsp;</p>
<p>Fine more examples on <a href="https://github.com/valums/file-uploader">github</a>.</p>
<p>&nbsp;</p>
<p>Some discussions on stackoverflow about known issues:</p>
<p>- <a href="http://stackoverflow.com/questions/4884920/mvc3-valums-ajax-file-upload">http://stackoverflow.com/questions/4884920/mvc3-valums-ajax-file-upload</a></p>
<p>- <a href="http://stackoverflow.com/questions/9882692/valums-file-uploader-doesnt-work-under-internet-explorer-9">http://stackoverflow.com/questions/9882692/valums-file-uploader-doesnt-work-under-internet-explorer-9</a></p>
<p><a href="http://stackoverflow.com/questions/4888632/mvc-valums-ajax-uploader-ie-doesnt-send-the-stream-in-request-inputstream">- http://stackoverflow.com/questions/4888632/mvc-valums-ajax-uploader-ie-doesnt-send-the-stream-in-request-inputstream</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/asp-net-mvc-file-uploads-using-valums-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic connection to MySQL database in Ruby on Rails (mysql2)</title>
		<link>http://maxivak.com/dynamic-connection-to-mysql-database-in-ruby-on-rails-mysql2/</link>
		<comments>http://maxivak.com/dynamic-connection-to-mysql-database-in-ruby-on-rails-mysql2/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 05:05:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=832</guid>
		<description><![CDATA[&#160; The following code connects to mysql database and retrieves some data. &#160; &#160;]]></description>
			<content:encoded><![CDATA[<p><span id="more-832"></span></p>
<p>&nbsp;</p>
<p>The following code connects to mysql database and retrieves some data.</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">'mysql2'</span>
&nbsp;
    db = <span class="re2">Mysql2::Client</span>.<span class="kw5">new</span><span class="br0">&#40;</span><span class="re3">:host</span> <span class="sy0">=&gt;</span> <span class="st0">'localhost'</span>, <span class="re3">:username</span> <span class="sy0">=&gt;</span> <span class="st0">'root'</span>, <span class="re3">:password</span><span class="sy0">=&gt;</span> <span class="st0">'password'</span>, <span class="re3">:database</span> <span class="sy0">=&gt;</span> <span class="st0">'db_name'</span>   <span class="br0">&#41;</span>
&nbsp;
    res = db.<span class="me1">query</span><span class="br0">&#40;</span><span class="st0">&quot;select * from users&quot;</span><span class="br0">&#41;</span>
    res.<span class="kw5">each</span> <span class="kw1">do</span> <span class="sy0">|</span>row<span class="sy0">|</span>
      col1 = row<span class="br0">&#91;</span><span class="st0">'id'</span><span class="br0">&#93;</span>
      col2 = row<span class="br0">&#91;</span><span class="st0">'email'</span><span class="br0">&#93;</span>
&nbsp;
   ..
    <span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/dynamic-connection-to-mysql-database-in-ruby-on-rails-mysql2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started with Redis in ASP.NET under Windows</title>
		<link>http://maxivak.com/getting-started-with-redis-and-asp-net-mvc-under-windows/</link>
		<comments>http://maxivak.com/getting-started-with-redis-and-asp-net-mvc-under-windows/#comments</comments>
		<pubDate>Mon, 26 Mar 2012 06:25:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[redis]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=807</guid>
		<description><![CDATA[Redis is one of the fastest and most feature-rich in-memory key value data stores. Install Redis server under Windows Redis was primarily developed on *NIX operating systems. If you&#8217;re looking to install on a *NIX platform the best place to download Redis is still from the redis project website. Start from Redis project download page. [...]]]></description>
			<content:encoded><![CDATA[<p>Redis is one of the fastest and most feature-rich in-memory key value data stores.</p>
<p><span id="more-807"></span></p>
<h2>Install Redis server under Windows</h2>
<p>Redis was primarily developed on <tt>*</tt>NIX operating systems. If you&#8217;re looking to install on a <tt>*</tt>NIX platform the best place to download Redis is still from the <a href="http://code.google.com/p/redis/" rel="nofollow">redis project website</a>.</p>
<p>Start from <a href="http://redis.io/download">Redis project download page</a>. The Redis project does not directly support win32/win64. Unofficial versions of Redis for Windows are available for downloading:</p>
<ul>
<li><a href="https://github.com/dmajkic/redis/">A Native win32/win64 port</a> created by DuÅ¡an MajkiÄ‡.</li>
<li><a href="https://gist.github.com/1439660">A patch released by Microsoft</a> based on <a href="https://github.com/joyent/libuv">libuv</a>.</li>
</ul>
<p>Seems like the Windows Redis port by DuÅ¡an MajkiÄ‡ is the most popular. You can download it from <a href="https://github.com/dmajkic/redis/downloads">here</a>.</p>
<p>&nbsp;</p>
<h3>Install Redis server as a service under Windows</h3>
<p>If you run Redis on Windows machine, the best option is to run each Redis instance as Windows service.</p>
<p>You can install the Windows Redis port by DuÅ¡an MajkiÄ‡ as follows:</p>
<p>- Download the <a href="https://github.com/dmajkic/redis/downloads">Windows Redis port</a> by DuÅ¡an MajkiÄ‡.</p>
<p>- Extract it to C:\Program Files\Redis or any other directory.</p>
<p>- Download <a href="https://github.com/kcherenkov/redis-windows-service">the Windows Redis service program by Konstantin Cherenkov</a> and put it into the same directory. So you have the file</p>
<p>C:\Program Files\Redis\RedisService.exe</p>
<p>- Run the following to install the service:</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">sc create Redis <span class="re2">start</span>= auto <span class="re2">DisplayName</span>= Redis <span class="re2">binpath</span>= <span class="st0">&quot;<span class="es1">\&quot;</span>C:\Program Files<span class="es1">\R</span>edis<span class="es1">\R</span>edisService.exe<span class="es1">\&quot;</span> <span class="es1">\&quot;</span>C:\Program Files<span class="es1">\R</span>edis<span class="es1">\r</span>edis.conf<span class="es1">\&quot;</span>&quot;</span></pre></div></div>
<p>where &#8220;Redis&#8221; can be any name of your service.</p>
<p>- Run the service. Go to your Windows Management / Services and run the service and/or make it run Automatically after the system starts.</p>
<p>- To uninstall service run</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">sc delete Redis</pre></div></div>Â </code></pre>
<p>where &#8220;Redis&#8221; is the name of the installed service.</p>
<p>&nbsp;</p>
<p>The instructions are taken from <a href="http://www.saltwebsites.com/2012/how-run-redis-service-under-windows">How to Run Redis as a Service under Windows</a> andÂ <a href="https://github.com/kcherenkov/redis-windows-service">Run Redis as Service on Windows</a>.</p>
<p>&nbsp;</p>
<h2>C# .NET Client Library</h2>
<p><strong>ServiceStack client library:</strong></p>
<p><a href="https://github.com/ServiceStack/ServiceStack.Redis">ServiceStack&#8217;s C# Redis Client</a> is an Open Source C# Redis client based on <a href="http://twitter.com/migueldeicaza">Miguel de Icaza</a> previous efforts with <a href="http://github.com/migueldeicaza/redis-sharp">redis-sharp</a>.</p>
<p>Download ServiceStack fromÂ  <a href="https://github.com/ServiceStack/ServiceStack.Redis/downloads">github page</a>.</p>
<p>Also you can download the Redis Client in any one of the following ways:</p>
<ul>
<li>Packaged by default in <a href="https://github.com/ServiceStack/ServiceStack/downloads">ServiceStack.dll</a></li>
<li>Available to download separately as a stand-alone <a href="https://github.com/ServiceStack/ServiceStack.Redis/downloads">ServiceStack.Redis.dll</a></li>
<li>As Source Code via Git: <code>git clone git://github.com/ServiceStack/ServiceStack.Redis.git</code></li>
<li>For those interested in having a GUI admin tool to visualize your Redis data should check out the <a href="http://www.servicestack.net/mythz_blog/?p=381">Redis Admin UI</a></li>
</ul>
<p>&nbsp;</p>
<p><strong>Overview ofÂ  ServiceStack</strong></p>
<p>ServiceStack has several interfaces:</p>
<ul>
<li><a href="http://www.servicestack.net/docs/framework/caching-options">ICacheClient</a> &#8211; If you are using Redis solely as a cache, you should bind to the <a href="http://www.servicestack.net/docs/framework/caching-options">ServiceStack&#8217;s common interface</a> as there already are In-Memory an Memcached implementations available in ServiceStack, allowing you to easily switch providers in-future.</li>
<li><a href="http://www.servicestack.net/docs/redis-client/iredisnativeclient-api">IRedisNativeClient</a> &#8211; For those wanting a low-level raw byte access (where you can control your own serialization/deserialization) that map 1:1 with Redis operations of the same name.</li>
</ul>
<p>For most cases if you require access to Redis-specific functionality you would want to bind to the interface below:</p>
<ul>
<li><a href="http://www.servicestack.net/docs/redis-client/iredisclient-api">IRedisClient</a> &#8211; Provides a friendlier, more descriptive API that lets you store values as strings (UTF8 encoding).</li>
<li><a href="http://www.servicestack.net/docs/redis-client/iredistypedclient-api">IRedisTypedClient</a> &#8211; created with <code>IRedisClient.GetTypedClient&lt;T&gt;()</code> &#8211; it returns a &#8216;strongly-typed client&#8217; that provides a typed-interface for all redis value operations that works against any C#/.NET POCO type.</li>
</ul>
<p>Classes:</p>
<ul>
<li>The <strong>RedisNativeClient</strong> exposes raw <strong>byte[]</strong> apis and does no marshalling and passes all values directly to redis.</li>
<li>The <strong>RedisClient</strong> assumes <strong>string</strong> values and simply converts strings to UTF8 bytes before sending to Redis</li>
<li>The <strong>RedisTypedClient</strong> provides a generic interface allowing you to add POCO values. The POCO types are serialized using <a href="http://www.servicestack.net/mythz_blog/?p=344">.NETs fastest JSON Serializer</a> which is then converted to UTF8 bytes and sent to Redis.</li>
</ul>
<p>Read the tutorial about using ServiceStack <a href="http://www.servicestack.net/docs/redis-client/redis-client">here</a>.</p>
<p>&nbsp;</p>
<h2>Examples</h2>
<p><strong>Caching using Redis and ServiceStack client library:</strong></p>
<p><div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;"><span class="kw1">using</span> <span class="co3">ServiceStack.Redis</span><span class="sy0">;</span>
<span class="co1">//</span>
RedisClient redisClient <span class="sy0">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span class="kw3">new</span></a> RedisClient<span class="br0">&#40;</span><span class="st0">&quot;localhost&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">// store some value in cache for one hour</span>
RedisClient.<span class="me1">Set</span><span class="br0">&#40;</span><span class="st0">&quot;mykey_1&quot;</span> <span class="sy0">+</span> name, 15, TimeSpan.<span class="me1">FromSeconds</span><span class="br0">&#40;</span>3600<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">// get typed value from cache</span>
<span class="kw4">int</span> valueFromCache <span class="sy0">=</span> RedisClient.<span class="me1">Get</span><span class="sy0">&lt;</span><span class="kw4">int</span><span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="st0">&quot;mykey_1&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// must be = 15</span></pre></div></div>
<p>&nbsp;</p>
<p>You can find a lot of examples on <a href="https://github.com/ServiceStack/ServiceStack.Examples">ServiceStack github page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/getting-started-with-redis-and-asp-net-mvc-under-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to pass multiple attributes to find_or_create_by in Rails 3 : the Ruby way</title>
		<link>http://maxivak.com/how-to-pass-multiple-attributes-to-find_or_create_by-in-rails-3-the-ruby-way/</link>
		<comments>http://maxivak.com/how-to-pass-multiple-attributes-to-find_or_create_by-in-rails-3-the-ruby-way/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 04:26:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=798</guid>
		<description><![CDATA[Ruby and Rails have many magic features like blocks/closures, duck typing/mix-ins/modules,Â  method_missing, poetry mode. One of the common tasks in programming web applications on Rails is finding a row in a table by several columns and creating a new row in case of row is missing. Find by one attribute This will find a row [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby and Rails have many magic features like blocks/closures, duck typing/mix-ins/modules,Â  method_missing, poetry mode.</p>
<p>One of the common tasks in programming web applications on Rails is finding a row in a table by several columns and creating a new row in case of row is missing.</p>
<p><span id="more-798"></span></p>
<p><strong>Find by one attribute</strong></p>
<p>This will find a row by &#8216;name&#8217; column in table &#8216;Products&#8217; and will create a new row if not found.</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;">Product.<span class="me1">find_or_create_by_name</span><span class="br0">&#40;</span><span class="st0">'name here'</span><span class="br0">&#41;</span></pre></div></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>If you want a new row to be created with many other attributes then you can pass them as a hash:</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;">Product.<span class="me1">find_or_create_by_name</span><span class="br0">&#40;</span><span class="st0">'namehere'</span>, <span class="re3">:category_id</span><span class="sy0">=&gt;</span>1,Â  <span class="re3">:enabled</span><span class="sy0">=&gt;</span>true, <span class="re3">:in_stock</span><span class="sy0">=&gt;</span>0, <span class="re3">:price</span><span class="sy0">=&gt;</span><span class="nu0">100</span><span class="br0">&#41;</span></pre></div></div>
<p>&nbsp;</p>
<p><strong>Find by multiple attributes</strong></p>
<p>If you want to find by multiple attributes you can use model&#8217;s methods like find_or_create_by_field1_and_field2(field1_value, field2_value).</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;">Product.<span class="me1">find_or_create_by_name_and_category_id</span><span class="br0">&#40;</span><span class="st0">'namehere'</span>, <span class="nu0">10</span><span class="br0">&#41;</span></pre></div></div>
<p>&nbsp;</p>
<p>If you want to <strong>pass multiple attributes</strong> to a new row to be created then you can use the following syntax:</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;">Product.<span class="me1">find_or_create_by_name_and_category_id</span><span class="br0">&#40;</span><span class="st0">'namehere'</span>, 10<span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>r<span class="sy0">|</span>
r.<span class="me1">price</span>= 100
r.<span class="me1">available</span> = <span class="kw2">true</span>
r.<span class="me1">enabled</span>=1
r.<span class="me1">in_stock</span>=0
<span class="kw1">end</span></pre></div></div>
<p>Notice that you cannot pass extra parameters to Product.find_or_create_by_name_and_category_id method because it expects only two parameters. Thanks Rails this method can be passed a block with your code and this code will be invoked for the new row to be initialized.</p>
<p>&nbsp;</p>
<p>Without all that Ruby&#8217;s magic the task could be achieved with the following code:</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;">attr = <span class="br0">&#123;</span> <span class="re3">:enabled</span><span class="sy0">=&gt;</span>true, <span class="re3">:in_stock</span><span class="sy0">=&gt;</span>0, <span class="re3">:price</span><span class="sy0">=&gt;</span><span class="nu0">100</span><span class="br0">&#125;</span>
row = Product.<span class="me1">find_by_name_and_category_id</span><span class="br0">&#40;</span><span class="st0">'namehere'</span>, 10<span class="br0">&#41;</span> <span class="sy0">||</span> Product.<span class="me1">create</span><span class="br0">&#40;</span>attr<span class="br0">&#41;</span></pre></div></div>
<p>&nbsp;</p>
<p>You must agree that this code looks not so elegant and concise.<br />
Details are <a href="https://github.com/maxivak/Rails-code-snippets/tree/master/find_and_create_by">here on Github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/how-to-pass-multiple-attributes-to-find_or_create_by-in-rails-3-the-ruby-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby metaprogramming and own custom attr_accessor</title>
		<link>http://maxivak.com/ruby-metaprogramming-and-own-attr_accessor/</link>
		<comments>http://maxivak.com/ruby-metaprogramming-and-own-attr_accessor/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 03:39:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[attr_accessor]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=770</guid>
		<description><![CDATA[Ruby is a dynamic language, that&#8217;s why you won&#8217;t get compile time type warnings/errors as you get in languages like C#. Ruby has a lot of metaprogramming features allowing you to create your own custom methods on the fly which can be used the same as att_accessor. dynamic method definition &#8211; define_method Use the following [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby is a dynamic language, that&#8217;s why you won&#8217;t get compile time type warnings/errors as you get in languages like C#.</p>
<p>Ruby has a lot of metaprogramming features allowing you to create your own custom methods on the fly which can be used the same as att_accessor.</p>
<p><span id="more-770"></span></p>
<p><strong>dynamic method definition &#8211; define_method</strong></p>
<p>Use the following code to define a new method in your class on the fly:</p>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> Myclass
define_method<span class="br0">&#40;</span><span class="st0">&quot;your_method_name_here&quot;</span><span class="br0">&#41;</span> <span class="kw1">do</span>
<span class="co1"># your code here</span>
<span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<p><strong>Custom attr_accessor method accessible from all classes</strong></p>
<p>To create a method that will be accesible from all classes you need to add code to the Class class. In Ruby a class is simply an object of class Class. Ruby provides a method<strong> class_eval</strong> that takes a string and evaluates it in the context of the current class, that is, the class from which you&#8217;re calling your attr_accessor.</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> <span class="kw1">Class</span>
<span class="kw1">def</span> my_custom_attr_method<span class="br0">&#40;</span>attr_name<span class="br0">&#41;</span>
<span class="co1"># your code here</span>
<span class="kw1">end</span>
<span class="kw1">end</span>
<span class="co1"># you can use this method in any of your classes</span>
<span class="kw1">class</span> Myclass
my_custom_attr_method <span class="re3">:attr1</span>
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<p><strong>attr_accessor</strong></p>
<p>Ruby&#8217;s method attr_accessor uses metaprogramming to create getters and setters for object attributes on the fly.</p>
<p>It creates the code like this:</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> <span class="kw1">Class</span>
&nbsp;
<span class="kw1">def</span> my_attr_accessor<span class="br0">&#40;</span><span class="sy0">*</span>args<span class="br0">&#41;</span>
&nbsp;
    <span class="co1"># iterate through each passed in argument...</span>
    args.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>arg<span class="sy0">|</span>
&nbsp;
        <span class="co1"># getter</span>
        <span class="kw2">self</span>.<span class="me1">class_eval</span><span class="br0">&#40;</span><span class="st0">&quot;def #{arg};@#{arg};end&quot;</span><span class="br0">&#41;</span>
&nbsp;
        <span class="co1"># setter</span>
        <span class="kw2">self</span>.<span class="me1">class_eval</span><span class="br0">&#40;</span><span class="st0">&quot;def #{arg}=(val);@#{arg}=val;end&quot;</span><span class="br0">&#41;</span>
&nbsp;
    <span class="kw1">end</span>
&nbsp;
<span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<h2>Examples</h2>
<p>Below is some examples of using metaprogramming.</p>
<p>&nbsp;</p>
<p><strong>Create custom attr_accessor to track the history of values<br />
</strong></p>
<p>Define a method attr_accessor_with_history that provides the same functionality as attr accessor but also tracks every value the attribute has ever had.</p>
<p>This method was taken from the home work task of the class &#8216;Software as a service&#8217; &#8211; <a href="https://www.coursera.org/saas/auth/welcome">https://www.coursera.org/saas/auth/welcome</a>.</p>
<p>&nbsp;</p>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> <span class="kw1">Class</span>
&nbsp;
  <span class="kw1">def</span> attr_accessor_with_history<span class="br0">&#40;</span>attr_name<span class="br0">&#41;</span>
    attr_name = attr_name.<span class="me1">to_s</span>
    attr_hist_name = attr_name<span class="sy0">+</span><span class="st0">'_history'</span>
&nbsp;
    <span class="co1">#getter</span>
    <span class="kw2">self</span>.<span class="me1">class_eval</span><span class="br0">&#40;</span><span class="st0">&quot;def #{attr_name};@#{attr_name};end&quot;</span><span class="br0">&#41;</span>
&nbsp;
    <span class="co1">#setter</span>
    <span class="kw2">self</span>.<span class="me1">class_eval</span> <span class="sy0">%</span>Q<span class="br0">&#123;</span>
      <span class="kw1">def</span> <span class="co1">#{attr_name}=(val)</span>
        <span class="co1"># add to history</span>
Â Â Â Â Â Â Â  @<span class="co1">#{attr_hist_name} = [nil] if @#{attr_hist_name}.nil?</span>
Â Â Â Â Â Â Â  @<span class="co1">#{attr_hist_name} &lt;&lt; val</span>
&nbsp;
       Â <span class="co1"># set the value itself</span>
        @<span class="co1">#{attr_name}=val</span>
      <span class="kw1">end</span>
&nbsp;
      <span class="kw1">def</span> <span class="co1">#{attr_hist_name};@#{attr_hist_name};end</span>
&nbsp;
                    <span class="br0">&#125;</span>
&nbsp;
  <span class="kw1">end</span>
&nbsp;
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<p>Usage of this accessor:</p>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> Foo
attr_accessor_with_history <span class="re3">:bar</span>
<span class="kw1">def</span> initialize<span class="br0">&#40;</span>some_bar_value<span class="br0">&#41;</span>
<span class="kw2">self</span>.<span class="me1">bar</span> = some_bar_value
<span class="kw1">end</span>
<span class="kw1">end</span>
 
f = Foo.<span class="me1">new</span>
f.<span class="me1">bar</span> = 3
f.<span class="me1">bar</span> = <span class="re3">:wowzo</span>
f.<span class="me1">bar</span> = <span class="st0">'boo!'</span>
<span class="kw3">puts</span> f.<span class="me1">bar_history</span> <span class="co1"># =&gt; [nil, 3, :wowzo, 'boo!']</span></pre></div></div>
<p>&nbsp;</p>
<p><strong>Note!</strong> You should use <strong>self.bar</strong> in the initializer method so it calls our custom setter to track the history.</p>
<p>If you write bar=some_bar_value then it will create a local variable &#8216;bar&#8217; that doesn&#8217;t do what is intended.</p>
<p>&nbsp;</p>
<h3>Strongly typed attr_accessor</h3>
<div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> Myclass
  <span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">typesafe_accessor</span><span class="br0">&#40;</span>name, type<span class="br0">&#41;</span>
&nbsp;
  define_method<span class="br0">&#40;</span>name<span class="br0">&#41;</span> <span class="kw1">do</span>
    instance_variable_get<span class="br0">&#40;</span><span class="st0">&quot;@#{name}&quot;</span><span class="br0">&#41;</span>
  <span class="kw1">end</span>
&nbsp;
  define_method<span class="br0">&#40;</span><span class="st0">&quot;#{name}=&quot;</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>value<span class="sy0">|</span>
    <span class="kw1">if</span> value.<span class="me1">is_a</span>? type
      instance_variable_set<span class="br0">&#40;</span><span class="st0">&quot;@#{name}&quot;</span>, value<span class="br0">&#41;</span>
    <span class="kw1">else</span>
      <span class="kw3">raise</span> <span class="kw4">ArgumentError</span>.<span class="me1">new</span><span class="br0">&#40;</span><span class="st0">&quot;Invalid Type&quot;</span><span class="br0">&#41;</span>
    <span class="kw1">end</span>
  <span class="kw1">end</span>
<span class="kw1">end</span>
&nbsp;
typesafe_accessor <span class="re3">:foo</span>, <span class="kw3">Integer</span>
&nbsp;
<span class="kw1">end</span>
&nbsp;
<span class="co1"># usage</span>
&nbsp;
f = Foo.<span class="me1">new</span>
f.<span class="me1">foo</span> = <span class="nu0">1</span>
f.<span class="me1">foo</span> = <span class="st0">&quot;bar&quot;</span> <span class="co1"># an exception thrown here!</span></pre></div></div>
<p>You can modify this code to create an accessor so it can be used in all classes. To do this &#8211; move this code to class Class and modify it a little like in the first example.</p>
<p>&nbsp;</p>
<p>This code is based on this explanation: <a href="http://stackoverflow.com/questions/7988410/attr-accessor-strongly-typed-ruby-on-rails">http://stackoverflow.com/questions/7988410/attr-accessor-strongly-typed-ruby-on-rails</a></p>
<p>&nbsp;</p>
<p><strong>Read more:</strong></p>
<p>-Â Meta-programming in Ruby &#8211; <a href="http://learnbysoft.blogspot.com/2010/10/meta-programming-in-ruby-part-1-alias.html"> http://learnbysoft.blogspot.com/2010/10/meta-programming-in-ruby-part-1-alias.html</a></p>
<p>- Screencasts on The Ruby Object Model and Metaprogramming &#8211; <a href="http://pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming">http://pragprog.com/screencasts/v-dtrubyom/the-ruby-object-model-and-metaprogramming</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/ruby-metaprogramming-and-own-attr_accessor/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Best Practices of Naming in Code</title>
		<link>http://maxivak.com/best-practices-of-naming-in-code/</link>
		<comments>http://maxivak.com/best-practices-of-naming-in-code/#comments</comments>
		<pubDate>Sat, 25 Feb 2012 21:45:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=767</guid>
		<description><![CDATA[Rules of naming of variables and methods: - Use meaningful names. For example &#8220;value&#8221;, &#8220;equals&#8221;, &#8220;data&#8221;, etc are not valid names of variables for any case. Variable name must define the exact explanation of its content. Method name must specify the exact action of the method and for most cases must start with a verb. [...]]]></description>
			<content:encoded><![CDATA[<p><span id="more-767"></span></p>
<p>Rules of naming of variables and methods:</p>
<p>- Use meaningful names.</p>
<p>For example &#8220;value&#8221;, &#8220;equals&#8221;, &#8220;data&#8221;, etc are not valid names of variables for any case.</p>
<p>Variable name must define the exact explanation of its content.</p>
<p>Method name must specify the exact action of the method and for most cases must start with a verb. (e.g. createPasswordHash)</p>
<p>Use meaningful names for method parameters, so it can documentate itself in case of no documentation.</p>
<p>- Use short enough and long enough variable names in each scope of code.</p>
<p>Generally length may be 1 char for loop counters, one word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals.</p>
<p>- Don&#8217;t reuse same variable name in the same class in different contexts: e.g. in method, constructor, class.</p>
<p>- Don&#8217;t use same variable for different purposes in a method, conditional etc. Create a new and different named variable instead.</p>
<p>- Don&#8217;t use non-ASCII chars in variable names.</p>
<p>- Use one natural language for naming, e.g. using mixed English and German names will be inconsistent and unreadable.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/best-practices-of-naming-in-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Developers spend their work week</title>
		<link>http://maxivak.com/how-developers-spend-their-work-week/</link>
		<comments>http://maxivak.com/how-developers-spend-their-work-week/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 06:54:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=760</guid>
		<description><![CDATA[&#160; &#160; Writing Code (programming, coding, hacking away) Overhead (Building, Deploying, Hardware, Software) Communication (Meetings, Chats, Teleconferences, etc) Problem-Solving (Debugging, Profiling, Performance Tuning) Firefighting (Crashes, Slowdowns, Security, etc) QA (Manual &#38; Automatic Testing, Code Reviews) Strategy (Architecture, Refactoring, Thinking) Process (Bureaucracy, Reporting, Time-keeping, etc) Procrastination (slashdot.org, reddit.com,. Twitter, Facebook) &#160; It means that Devs spend [...]]]></description>
			<content:encoded><![CDATA[<p><span id="more-760"></span></p>
<p>&nbsp;</p>
<p><a href="http://maxivak.com/wp-content/uploads/2012/02/dev-hours.png"><img class="aligncenter size-full wp-image-764" title="dev-hours" src="http://maxivak.com/wp-content/uploads/2012/02/dev-hours.png" alt="" width="509" height="346" /></a></p>
<p>&nbsp;</p>
<ul>
<li>Writing Code (programming, coding, hacking away)</li>
<li>Overhead (Building, Deploying, Hardware, Software)</li>
<li>Communication (Meetings, Chats, Teleconferences, etc)</li>
<li>Problem-Solving (Debugging, Profiling, Performance Tuning)</li>
<li>Firefighting (Crashes, Slowdowns, Security, etc)</li>
<li>QA (Manual &amp; Automatic Testing, Code Reviews)</li>
<li>Strategy (Architecture, Refactoring, Thinking)</li>
<li>Process (Bureaucracy, Reporting, Time-keeping, etc)</li>
<li>Procrastination (slashdot.org, reddit.com,. Twitter, Facebook)</li>
</ul>
<p>&nbsp;</p>
<p>It means that</p>
<ul>
<li>Devs spend<strong> less time writing code</strong> than you might think. The median is 15 hours, programmers spend about 3 hours each work day writing code.</li>
<li>Devs spend <strong>more time on non-development activities</strong> than you might think. For each coding hour, devs spend nearly 30min in meetings, reporting, writing emails and dealing with timesheets</li>
<li>Devs spend more time fighting fires than building solutions</li>
</ul>
<p>&nbsp;</p>
<p>Information is taken from</p>
<p><a href="http://zeroturnaround.com/blog/developer-productivity-report-part-1-developer-timesheet/">http://zeroturnaround.com/blog/developer-productivity-report-part-1-developer-timesheet/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/how-developers-spend-their-work-week/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP vs Ruby vs Python (Infographic)</title>
		<link>http://maxivak.com/php-vs-ruby-vs-python-infographic/</link>
		<comments>http://maxivak.com/php-vs-ruby-vs-python-infographic/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 16:09:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=741</guid>
		<description><![CDATA[Ruby: - elegant code - not easy to learn PHP: - easy to learn for former C programmers &#160;]]></description>
			<content:encoded><![CDATA[<p><span id="more-741"></span></p>
<p><strong>Ruby:</strong></p>
<p>- elegant code</p>
<p>- not easy to learn</p>
<p><strong>PHP:</strong></p>
<p>- easy to learn for former C programmers</p>
<p><a href="http://maxivak.com/wp-content/uploads/2012/01/php-python-ruby-infographie-gnd-geek.png"><img class="aligncenter size-large wp-image-742" title="php-python-ruby-infographie-gnd-geek" src="http://maxivak.com/wp-content/uploads/2012/01/php-python-ruby-infographie-gnd-geek-184x1024.png" alt="" width="184" height="1024" /></a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/php-vs-ruby-vs-python-infographic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to determine if a record was created or updated in after_save callback (Ruby on Rails)</title>
		<link>http://maxivak.com/how-to-determine-if-a-record-was-created-or-updated-in-after_save-callback-ruby-on-rails/</link>
		<comments>http://maxivak.com/how-to-determine-if-a-record-was-created-or-updated-in-after_save-callback-ruby-on-rails/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 13:43:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=738</guid>
		<description><![CDATA[sometimes in after_save callback it is needed to determine if a record was created or updated without the use of after_create callback. Rails has no direct way for doing this. The solution can be as follows: &#160;]]></description>
			<content:encoded><![CDATA[<p>sometimes in after_save callback it is needed to determine if a record was created or updated without the use of after_create callback.</p>
<p><span id="more-738"></span></p>
<p>Rails has no direct way for doing this.</p>
<p>The solution can be as follows:</p>
<div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw1">def</span> <span class="kw5">before_save</span>
  <span class="re1">@was_a_new_record</span> = new_record?
  <span class="kw2">return</span> <span class="kw2">true</span>
<span class="kw1">end</span>
&nbsp;
<span class="kw1">def</span> <span class="kw5">after_save</span>
  <span class="kw1">if</span> <span class="re1">@was_a_new_record</span>
    ...
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/how-to-determine-if-a-record-was-created-or-updated-in-after_save-callback-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>update_attribute, update_attributes and update_column (Ruby on Rails)</title>
		<link>http://maxivak.com/update_attribute-and-update_attributes-ruby-on-rails/</link>
		<comments>http://maxivak.com/update_attribute-and-update_attributes-ruby-on-rails/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 13:12:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=732</guid>
		<description><![CDATA[update_attribute Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default. Also note that Validation is skipped. CallbacksÂ are [...]]]></description>
			<content:encoded><![CDATA[<p><span id="more-732"></span></p>
<p><strong>update_attribute</strong></p>
<p>Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.</p>
<p>Also note that</p>
<ul>
<li>Validation is skipped.</li>
<li><a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html">Callbacks</a>Â are invoked.</li>
<li>updated_at/updated_on column is updated if that column is available.</li>
<li>Updates all the attributes that are dirty in this object.</li>
</ul>
<p><strong>update_attributes</strong></p>
<p>Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.</p>
<div>
<p><strong>Examples:</strong></p>
<div id="wpshdo_19" class="wp-synhighlighter-outer"><div id="wpshdt_19" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_19"></a><a id="wpshat_19" class="wp-synhighlighter-title" href="#codesyntax_19"  onClick="javascript:wpsh_toggleBlock(19)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_19" onClick="javascript:wpsh_code(19)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_19" onClick="javascript:wpsh_print(19)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_19" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw4">Object</span>.<span class="me1">update_attribute</span><span class="br0">&#40;</span><span class="re3">:only_one_field</span>, <span class="st0">&quot;Some Value&quot;</span><span class="br0">&#41;</span>
<span class="kw4">Object</span>.<span class="me1">update_attributes</span><span class="br0">&#40;</span><span class="re3">:field1</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;value&quot;</span>, <span class="re3">:field2</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;value2&quot;</span>, <span class="re3">:field3</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;value3&quot;</span><span class="br0">&#41;</span>
<span class="kw4">Object</span>.<span class="me1">update_attributes</span><span class="br0">&#40;</span>params<span class="br0">&#91;</span><span class="re3">:user</span><span class="br0">&#93;</span><span class="br0">&#41;</span></pre></div></div>
<p>All of these will update an object in a database without having to explicitly tell AR to update.</p>
<p><strong>update_attribute</strong> can update only one column with this andÂ <code>updated_at</code>Â isÂ <strong>not</strong>Â updated.</p>
</div>
<p>&nbsp;</p>
<p><strong>Validation</strong></p>
<p>Use <strong>update_attribute</strong> to skip callbacks and validations.</p>
<p>The difference between two isÂ <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002336" rel="nofollow">update_attribute</a>Â useÂ <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002329" rel="nofollow">save(false)</a>Â where asÂ <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002337" rel="nofollow">update_attributes</a>Â usesÂ <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002329" rel="nofollow">save</a>Â or you can sayÂ <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002329" rel="nofollow">save(true)</a>.</p>
<p>&nbsp;</p>
<p><strong>Callbacks</strong></p>
<p>If perform_validation is false while calling save then it skips validation, and it also means that all the before_*Â <a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html#M002139" rel="nofollow">callbacks</a> associated with save.</p>
<p>&nbsp;</p>
<h2>Update attributes without saving</h2>
<p><strong>update_column</strong>(name, value)</p>
<p>Updates a single attribute of an object, without calling save.</p>
<ul>
<li>Validation is skipped.</li>
<li><a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html">Callbacks</a>Â are skipped.</li>
<li>updated_at/updated_on column is not updated if that column is available.</li>
</ul>
<p>Raises anÂ <code>ActiveRecordError</code>Â when called on new objects, or when theÂ <code>name</code>Â attribute is marked as readonly.</p>
<p>&nbsp;</p>
<p><strong>.attributes property</strong></p>
<p>Use .attributes to update attributes of the model without saving it to a database.</p>
<div id="wpshdo_20" class="wp-synhighlighter-outer"><div id="wpshdt_20" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_20"></a><a id="wpshat_20" class="wp-synhighlighter-title" href="#codesyntax_20"  onClick="javascript:wpsh_toggleBlock(20)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_20" onClick="javascript:wpsh_code(20)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_20" onClick="javascript:wpsh_print(20)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_20" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="re1">@car</span> = Car.<span class="kw5">new</span><span class="br0">&#40;</span><span class="re3">:some_field</span><span class="sy0">=&gt;</span><span class="st0">'value'</span>, <span class="re3">:another_field</span><span class="sy0">=&gt;</span><span class="st0">'value'</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">#</span>
<span class="re1">@car</span>.<span class="me1">attributes</span> = <span class="br0">&#123;</span>:title <span class="sy0">=&gt;</span> <span class="st0">'Toyota'</span>, <span class="re3">:year</span> <span class="sy0">=&gt;</span> 2005, <span class="re3">:used</span><span class="sy0">=&gt;</span>false<span class="br0">&#125;</span>
&nbsp;
<span class="co1">#other processing</span>
..
&nbsp;
<span class="co1">#finally, save to DB</span>
<span class="re1">@car</span>.<span class="kw5">save</span></pre></div></div>
<p>&nbsp;</p>
<p><strong>Ref.</strong><br />
<strong>Discussions on StackOverflow:</strong><br />
<a href="http://stackoverflow.com/questions/2778522/rails-update-attribute-vs-update-attributes">http://stackoverflow.com/questions/2778522/rails-update-attribute-vs-update-attributes</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/update_attribute-and-update_attributes-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

