Wednesday, July 16, 2008

RAILS 2.1 - :through

Active Record has an interesting feature :through. It helps to create the association through the association. For example:

class Court < ActiveRecord::Base
has_many :court_users
has_many users :through=> :court_users
end

class CourtUser < ActiveRecord::Base
belongs_to :court
belongs_to :user
end

In the above example court has_many users through the court_users since the association between court and the user is exists only in the court_users table.

Before 2.1, only has_many method has :through. Now, has_one method has the option :through

No comments: