Skip to content

Instantly share code, notes, and snippets.

@fabianoalmeida
Created November 25, 2011 14:05
Show Gist options
  • Save fabianoalmeida/1393599 to your computer and use it in GitHub Desktop.
Save fabianoalmeida/1393599 to your computer and use it in GitHub Desktop.
Polymorphic Association Example
### PaymentWay
class PaymentWay < ActiveRecord::Base
belongs_to :payable, :polymorphic => true
end
class CreatePaymentWays < ActiveRecord::Migration
def change
create_table :payment_ways do |t|
t.string :name
t.references :payable, :polymorphic => true # Will create two columns => :payable_id and :payable_type
t.timestamps
end
end
end
### CreditCard
class CreditCard < ActiveRecord::Base
has_many :payment_ways, :as => :payable
belongs_to :company
belongs_to :flag
end
class CreateCreditCards < ActiveRecord::Migration
def change
create_table :credit_cards do |t|
t.string :name, :null => false
t.references :company, :null => false
t.references :flag, :null => false
t.timestamps
end
end
end
@Prajapatisantu
Copy link

need full project of this association

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment