2008年10月27日 星期一

Has_one with :source_type

http://www.javaeye.com/topic/129195


class Client < ActiveRecord::Base
has_many :contact_cards
has_many :business_contacts,
:through => :contact_cards,
:source => :contact, <= 跟 as => :this_item 的this_item 一樣
:source_type => 'Business'
end

class Business < ActiveRecord::Base
has_many :contact_cards, :as => :contact
end

class ContactCard < ActiveRecord::Base
belongs_to :client
belongs_to :contact, :polymorphic => true
end

User.first.business_cards <= 第一個user的contact card of bussiness category

沒有留言: