Monday, September 15, 2008

Active Record tips and tricks

Found a great post about active record tips and trick here:
http://m.onkey.org/2008/9/15/active-record-tips-and-tricks

One more thought about the use of concerned_with. I prefer to use require_dependency directly. So I don't need to add and load concerns.rb for every new Rails project and get more flexible to organize related files.

For example:


class Listing < ActiveRecord::Base
require_dependency "listing/property_related"
require_dependency "listing/price_related"
require_dependency "listing/contact_related"
require_dependency "listing/address_related"
require_dependency "full_text_search/listing_config"
....
end



When you want to separate full text search config code to another file, remember to inherit the class from ActiveRecord::Base in order to let enable ThinkingSphinx plugin or UltraSphinx plugin methods.

For example:


# full_text_search/listing_config.rb
class Listing < ActiveRecord::Base
define_index do # ThinkingSphinx plugin
...
end
end

No comments: