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
class Book < ActiveRecord::Base ; end
book = Book.find( 1 )
book.to_csv
Example 2, only exporting certain fields
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
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
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
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
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
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
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
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
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”:http://rubyforge.org/frs/?group_id=2113
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/
blog comments powered by Disqus