ActiveRecord::Extensions 0.4.0 Released!
on February 12, 2007 @ 02:22 AM

ActiveRecord::Extensions 0.4.0 is released!

Updates

  • added to_csv functionality (works with belongs_to, has_one and has_many relationships)
  • added temporary table functionality
  • added foreign key functionality

to_csv functionality

Example 1, exporting all fields

1
2
3
4
 class Book < ActiveRecord::Base ; end

 book = Book.find( 1 )
 book.to_csv

Example 2, only exporting certain fields

1
2
3
4
 class Book < ActiveRecord::Base ; end

 book = Book.find( 1 )
 book.to_csv( :only=>%W( title isbn )

Example 3, exporting a model including a belongs_to association

1
2
3
4
5
6
 class Book < ActiveRecord::Base
   belongs_to :author
 end

 book = Book.find( 1 )
 book.to_csv( :include=>:author )

This also works for a has_one relationship. The :include option can also be an array of has_one/belongs_to associations. This by default includes all fields on the belongs_to association.

Example 4, exporting a model including a has_many association

1
2
3
4
5
6
 class Book < ActiveRecord::Base
   has_many :tags
 end

 book = Book.find( 1 )
 book.to_csv( :include=>:tags )

This by default includes all fields on the has_many assocaition. This can also be an array of multiple has_many relationships. The array can be mixed with has_one/belongs_to associations array as well. IE: :include=>[ :author, :sales ]

Example 5, nesting associations

1
2
3
4
5
6
7
8
9
 class Book < ActiveRecord::Base
   belongs_to :author
   has_many :tags
 end

 book = Book.find( 1 )
 book.to_csv( :include=>{
                 :author => { :only=>%W( name ) },
                 :tags => { :only=>%W( tagname ) } )

Example 6, using Arrays returned by find

1
2
3
4
5
6
7
8
9
 class Book < ActiveRecord::Base
   belongs_to :author
   has_many :tags
 end

 books = Book.find( :all )
 books.to_csv( :include=>{
                 :author => { :only=>%W( name ) },
                 :tags => { :only=>%W( tagname ) } )

Temporary Table Support

Example 1, using defaults

1
2
3
 class Project < ActiveRecord::Base ; end

 Project.create_temporary_table

This creates a temporary table named ‘temp_projects’ and creates a constant name TempProject. The table structure is copied from the projects table.

Example 2, using :table_name and :model options


  Project.create_temporary_table :table_name=>'my_projects', :model=>'MyProject'

This creates a temporary table named ‘my_projects’ and creates a constant named MyProject. The table structure is copied from the projects table.

Example 3, using :like


  ActiveRecord::Base.create_temporary_table :like=>Project

This is the same as calling Project.create_temporary_table.

Example 4, using block form

1
2
3
  Project.create_temporary_table do |t|
    # ...
  end

Using the block form will automatically drop the temporary table when the block exits. t which is passed into the block is the temporary table class. In the above example t equals TempProject. The block form can be used with all of the available options.

Foreign Key Support

Enables support for enabling and disabling foreign keys for the underlying database connection for ActiveRecord. This can be used with or without block form. This also uses the connection attached to the model.

Example 1, without block form

1
2
  Project.foreign_keys.disable
  Project.foreign_keys.enable

If you use this form you have to manually re-enable the foreign keys.

Example 2, with block form

1
2
3
4
5
6
7
  Project.foreign_keys.disable do
    # ...
  end

 Project.foreign_keys.enable do
   # ...
 end

If you use the block form the foreign keys are automatically enabled or disabled when the block exits. This currently does not restore the state of foreign keys to the state before the block was entered.

Download It Now!

ActiveRecord::Extensions 0.4.0

Check it out from SVN into your Rails project

svn co svn://rubyforge.org/var/svn/arext/tags/ar-extensions-0.4.0 vendor/plugins/

  1. Gregory Brown 02.12.07 / 04AM
    Hi, Have you looked at acts_as_reportable[0]? It's still under development but it does stuff similar to the to_csv method you're showing here. [0] http://stonecode.svnrepository.com/acts_as_reportable/
  2. Zach Dennis 02.12.07 / 13PM
    Gregory , I've been watching the development of acts_as_reportable the past few weeks. It looks pretty nice how it functions and takes advantage of Ruport. I probably won't do much to the to_csv functionality besides refactor at this point. IIRC, acts_as_reportable doesn't currently handle has_many relationships. Do you have plans for this, if not I'm guessing you'll accept patches?
  3. Gregory Brown 02.13.07 / 00AM
    You might wish to ask this on the Ruport list[0]. I stopped maintaining acts_as_reportable a few months ago, and Dudley Flanders and Mike Milner have stepped up to do that... Mike mentioned a few days ago that we now handle 'all types of associations', but i'm not sure about the details. But we're always accepting patches. I was mostly mentioning AAR because it'd be good to share ideas, and maybe offer a consistent interface between acts_as_reportable and ActiveRecord::Extensions as far as CSV genertion is concerned. This project is looking very cool though, so I'll definitely keep an eye on it! [0] http://list.rubyreports.org
  4. Gabe 03.02.07 / 21PM
    I just found this project, and I am pretty excited about it. I'm a little concerned about the low activity on RubyForge because I know from firsthand experience how easy it is for a project to die without a community. However, you've been doing steady releases, and the value is tremendous if you can keep up the momentum. I'm willing to help as I can. I'm interested in all the DB specific functionality, but the real killer features for me are the Like, Comparison and Regexp features. Having those available in conditions hashes is huge because I'm working on an app heavily focused around advanced search, and the ability to merge all those conditions in a Hash will be so much better than trying to construct SQL strings myself. I took a look at EZWhere, but it seems like it introduces more complexity than I want in a situation where AR Hash conditions get my 80% of where I need to be. I'll be putting this stuff through some pretty heavy use and posting feedback. I'd like to see arext gain prominence in the community and who knows, maybe some of this stuff makes it into Rails core. Thanks for your hard work.