acts_as_list - null scope attribute value

Some time ago I ran into performance problems connected with acts_as_list.

My acts_as_list in EmailAddress model was defined as follows:

acts_as_list :scope => :user

Unfortunately, most email addresses in database had user_id set to null.

Acts_as_list takes all records from table which has null scope attribute and creates one huge list from all of them… Ups… In my situation it means almost

SELECT * FROM email_addresses
query on each new EmailAddress creation what causes very low performance.

Here is my fix. It makes possible to write:

acts_as_list :scope => :user, :ignore_nil => true

After doing so, newly created EmailAddress will not be added to any list unless user is set to non null.

In my opinion original behaviour is quite odd as acts_as_list supports listable records which are not on any list…

Posted by Kacper Bielecki Tue, 21 Apr 2009 18:49:00 GMT