Category: activerecord
Polymorphic Single Table Inheritance (STI)
March 30th, 2010, Comments Off
Migration:
create_table :addresses do |t|
….
t.references :addressable, :polymorphic => true
t.string :type
t.timestamps
end
Address Models:
class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end
class InvoiceAddress < [...]
Correct Format Plugin Released
July 4th, 2009, Comments Off
I have released a new Plugin for Rails, called Correct-Format.
Github URL: github.com/mendable/correct-format
This plugin allows you to automatically correct simple user input mistakes
and format user-input without raising an ActiveRecord Error and without
inserting inconsistently formatted data into your database. Using this
plugin will enhance the usability and user-friendlyness of your application
and increase your data integrity.
You can automatically:
Make a [...]
Understand Named_scope in 60 seconds
June 28th, 2009, 1 Comment
Named scope was introduced in Rails 2.x, it allows you to filter a selection of records easily and repeatedly, without having to write finder SQL all the time.
class Customer < ActiveRecord::Base
has_many :carts
end
class Cart < ActiveRecord::Base
belongs_to :customer
named_scope :completed, :conditions => ["NOT ISNULL(completed_at)"]
end
Example of self-model scoping and finding:
# All completed carts [...]