Posts Tagged ‘activerecord’

ActiveRecord and Transactions!

Wednesday, October 29th, 2008

If you ever have to do multiple actions with activerecord, you should group it into a transaction like so:

Post.transaction do
  posts.each do |post|
    post = Post.new(:body=>'Whatever')
    post.save!
  end
end

This will rollback too if the save fails.

Bulk update using ActiveRecord without SQL!

Thursday, October 23rd, 2008

Here is a cool way you can update columns in a table without writing any SQL using ActiveRecord. Suppose you have a form with checkboxes that are for deleting posts for the ones that are checked:

<input type="checkbox" name="posts[]" value="<%= post.id %>">

Now you can update this in the method of your controller that this form posts to:

Post.update_all({:removed=>true}, {:id=>params[:posts]})

ActiveRecord will take of the fact that you want to update 1 record or multiple!

ActiveRecord counting with associations

Wednesday, October 22nd, 2008

The other day I had to display the number of children items an object had so I decided to do the following:

# Controller
@customers = Customer.find(:all, :include=>[:receive_payments])
 
#View
<%= customer.receive_payments.count %>

While this worked for what I needed, it executed the following sql every iteration:

SELECT count(*) AS count_all FROM "receive_payments" WHERE ("receive_payments".customer_ref_list_id = E'850000-1071531366')

I thought this wasn’t right considering I used an association, however when you use count, it forces the use of count(*) on the database. This is where size comes in!!!!

<%= customer.receive_payments.size %>

Now we get the same results without the extra database counts!

Instant Rails Scaling through Asynchronous(Non-blocking) ActiveRecord

Thursday, September 4th, 2008

The guys over at NeverBlock have released a database adapter for Rails application that will severely increase the performance of ActiveRecord. Its also really easy to integrate into your application. Heres how:

Add a line to environment.rb for mongrel or thin servers:

require 'never_block/servers/thin'

or

require 'never_block/servers/mongrel'

Change the adapter in database.yml:

adapter: neverblock_postgresql

or

adapter: neverblock_mysql

You can also specify the number of connections (default of 4):

connections: 12

More information, along with benchmarks, can be found here.

ActiveRecord conditions with association from hash

Friday, August 29th, 2008

I’m sure you all know how to use the :conditions attribute when using ActiveRecord:

User.find(:all, :conditions=>['active = ?', true])

And you may even use associations this way:

User.find(:all, :include=>[:photos], :conditions=>['photos.removed = ? and users.active = ?', false, true])

But did you know that you can do this easier through hashes?

User.find(:all, :conditions=>{:active=>true})
User.find(:all, :include=>[:photos], :conditions=>{'photos.removed'=>false, 'users.active'=>true})

Nothing special there, but I thought it was pretty cool. One thing you have to remember when using associations, is to include that model.