Veerasundaravel's Ruby on Rails Weblog

November 26, 2014

Rails find_each method with order option

Filed under: Rails3, Ruby, Ruby On Rails — Tags: , , , , , , , — Veerasundaravel @ 9:36 pm
Rails find_each or find_in_batches methods are looping through a collection of records from the database.
This batch processing methods allow us to work with the records in batches, thereby greatly reducing memory consumption.
Person.find_each(:conditions => "age > 21") do |person|
  person.party_all_night!
end
But major drawback with these methods is  - ordering/sorting the records by primary key id, hence we cannot specify our own order_by.
order_by(:created_at).find_each == FAIL!!!
class ActiveRecord::Base
  # normal find_each does not use given order but uses id asc
  def self.find_each_with_order(options={})
    raise "offset is not yet supported" if options[:offset]

    page = 1
    limit = options[:limit] || 1000

    loop do
      offset = (page-1) * limit
      batch = find(:all, options.merge(:limit=>limit, :offset=>offset))
      page += 1

      batch.each{|x| yield x }

      break if batch.size < limit
    end
  end
end

July 2, 2014

Rails – Force update ActiveRecord updated_at column

Filed under: Ruby On Rails — Tags: , , , , , , — Veerasundaravel @ 12:11 am

When we usually modify any specific column/field in our model, Active-record will check whether the corresponding value is new or not.

If it is new then only it will create a DB query and update that value with current timestamp in updated_at column, if there is no change in existing value means it will only fire commit query only. There wont be any change in updated_at column.

So if we want modify the timestamp of updated_at field without updating any field means, we can follow any of below two methods:

Method 1:
user.update_attributes(:name => “same old name”, :updated_at => Time.now)

Method 2:
user.touch

Both of above two methods will modify updated_at value with current timestamps even if there is any change in other fields or not.

 

November 13, 2011

Localhost alternates for subdomain

Filed under: Ruby On Rails — Tags: , , , , , , , , — Veerasundaravel @ 2:04 am

Most of we Rails developers, used to check our Rails app using http://localhost:3000/. But this particular url have the  following issues.

1. We can’t add/check subdomain with localhost like http://company1.localhost:3000/

2. Some of external API will not work when we are using localhost.

3. More over using system hostname for subdomain also requires too much configuration in host files.

Best way to solve this:

smackaho.st and lvh.me provides good option of using localhost with subdomain etc. The two domains are pointed to localhost only. If we go to http://lvh.me:3000/ we’ll see the homepage of our application because lvh.me resolves to the IP address 127.0.0.1.

Checkout all the below urls. all will open your application homepage.

http://smackaho.st:3000/
http://abc.smackaho.st:3000/
http://lvh.me:3000/
http://xyz.lvh.me:3000/


Related Articles:

tbaggery – Smack a Ho.st
ASCIIcasts – Subdomain in Rails3

Older Posts »

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 123 other followers

%d bloggers like this: