<?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>Ruby on Rails Notes &#187; models</title> <atom:link href="http://railsnotes.com/tag/models/feed/" rel="self" type="application/rss+xml" /><link>http://railsnotes.com</link> <description>A code-heavy brain dump of stuff I come across working on Ruby on Rails projects including Models, ActiveRecord, Views, Controllers, RESTful rails, deployment, server stuff, etc.</description> <lastBuildDate>Sun, 29 Aug 2010 23:45:16 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <item><title>Number to Currency Inside Models</title><link>http://railsnotes.com/110-number-to-currency-inside-models/</link> <comments>http://railsnotes.com/110-number-to-currency-inside-models/#comments</comments> <pubDate>Fri, 03 Apr 2009 09:13:05 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[Ruby & Rails Core]]></category> <category><![CDATA[models]]></category> <category><![CDATA[money]]></category><guid
isPermaLink="false">http://railsnotes.wordpress.com/?p=110</guid> <description><![CDATA[I was working on something where I needed to use number_to_currency inside a model. I wanted @service.price to use the amount &#38; setup_amount fields to output something like this: &#8220;$120.00 setup + $23/month&#8221; This is a problem since number_to_currency is part of the ActionView module and not available inside models. Some say that it violates [...]]]></description> <content:encoded><![CDATA[<p></p><p>I was working on something where I needed to use number_to_currency inside a model. I wanted @service.price to use the amount &amp; setup_amount fields to output something like this: &#8220;$120.00 setup + $23/month&#8221;</p><p>This is a problem since number_to_currency is part of the ActionView module and not available inside models. Some say that it violates MVC principles since in a way, you&#8217;re messing with view-related code inside a model. That part is up for debate. In anycase, ravocx from the rubyonrails IRC channel put together a small snippet that extends Fixnum so you can call some_number.to_currency on it.</p><p>Here&#8217;s how it works:</p><ol><li>Make a folder RAILS ROOT/lib/core_extensions</li><li>In there, create a file named fixenum_extensions.rb</li><li>Copy/paste the code below into that file.</li><li>In your environment.rb file, put this line:</li><pre>RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)</pre><li>And this line:</li><pre>require "#{RAILS_ROOT}/lib/core_extensions/fixnum_extensions"</pre><li>And restart your application</li></ol><p>###</p><pre>require 'rubygems'
require 'action_view'

class Fixnum
  def to_currency(options = {})
    ActionView::Base.new.number_to_currency(self, options)
  end
end</pre><p>###</p><p>For my purpose, I used end deciding to leave the code in the helper for now but I may move to this method later.</p><p>DISCLAIMER: If you do use this approach, you might consider moving this code into a module and using that module to extend Fixnum.</p> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/110-number-to-currency-inside-models/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Fetching all records corresponding to a set of ids, ignoring invalid ids</title><link>http://railsnotes.com/27-fetching-all-records-corresponding-to-a-set-of-ids-ignoring-invalid-ids/</link> <comments>http://railsnotes.com/27-fetching-all-records-corresponding-to-a-set-of-ids-ignoring-invalid-ids/#comments</comments> <pubDate>Wed, 01 Oct 2008 05:05:24 +0000</pubDate> <dc:creator>Melvin Ram</dc:creator> <category><![CDATA[models]]></category> <category><![CDATA[database]]></category><guid
isPermaLink="false">http://railsnotes.wordpress.com/?p=27</guid> <description><![CDATA[Conversation from #rubyonrails IRC channel pipegeek: Is there a straightforward way, given a list of ids, fetch all corresponding records, ignoring invalid ids? pipegeek: ie, something like Model.find([1,2,3,4,5]), but without the requirement that all the ids be matched pipegeek: err, should clarify (if it isn&#8217;t already obvious) I&#8217;m speaking about activerecord therrg: pipegeek: Model.find_all_by_id([1,2,3,4,5]) does [...]]]></description> <content:encoded><![CDATA[<p></p><p><strong>Conversation from #rubyonrails IRC channel</strong></p><p>pipegeek:<span> </span>Is there a straightforward way, given a list of ids, fetch all corresponding records, ignoring invalid ids?</p><p>pipegeek:<span> </span>ie, something like Model.find([1,2,3,4,5]), but without the requirement that all the ids be matched</p><p>pipegeek:<span> </span>err, should clarify (if it isn&#8217;t already obvious) I&#8217;m speaking about activerecord</p><p>therrg:<span> </span>pipegeek: Model.find_all_by_id([1,2,3,4,5]) does what you want</p><p>pipegeek:<span> </span>oh!  Damn, I feel foolish for not having checked that&#8212;figured the find_all_by_ methods behaved the same way</p><p>pipegeek:<span> </span>thanks, therrg</p><p>therrg:<span> </span>yw</p><p>melvinram:<span> </span>btw, what is your usage scenario?</p><p>melvinram:<span> </span>trying to see when that would be useful</p><p>pipegeek:<span> </span>Well, I have a list of ids generated much earlier by some other tool that added a bunch of records</p><p>pipegeek:<span> </span>I need to do something to all of those records.  Only, it&#8217;s possible that some have been DELETEd in the meantime</p><p>melvinram:<span> </span>ahe i see</p> ]]></content:encoded> <wfw:commentRss>http://railsnotes.com/27-fetching-all-records-corresponding-to-a-set-of-ids-ignoring-invalid-ids/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 2/16 queries in 0.040 seconds using disk

Served from: railsnotes.com @ 2010-09-09 07:33:39 -->