Created
November 25, 2011 14:05
-
-
Save fabianoalmeida/1393599 to your computer and use it in GitHub Desktop.
Polymorphic Association Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need full project of this association