Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## transaction.rb
- class Transaction < ActiveRecord::Base
- has_one :seller_agent
- has_one :agent, :through => :seller_agent
- end
- ## seller_agent.rb
- class SellerAgent < ActiveRecord::Base
- belongs_to :agent
- belongs_to :transaction
- end
- ## agent.rb
- class Agent < ActiveRecord::Base
- has_many :seller_agents
- has_many :transactions, :through => :seller_agents
- end
- ## summary of ./script/console output
- Loading development environment (Rails 2.1.1)
- >> tr = Transaction.new
- => #<Transaction id: nil, trans_number: nil, created_at: nil, updated_at: nil>
- >> tr.trans_number = 2221
- => 2221
- >> tr.save
- => true
- >> tr.agent
- >> ag = Agent.new
- => #<Agent id: nil, username: nil, created_at: nil, updated_at: nil>
- >> ag.username = 'justme'
- => "justme"
- >> ag.save
- => true
- >> tr.agent = ag
- => #<Agent id: 1, username: "justme", created_at: "2008-10-15 01:47:36", updated_at: "2008-10-15 01:47:36">
- >> tr.agent
- => nil
- >> tr.save
- => true
- >> tr.agent
- => nil
- >> tr.seller_agent
- => nil
- >> SellerAgent.find :all
- => []
- >> sa = SellerAgent.new
- => #<SellerAgent id: nil, agent_id: nil, transaction_id: nil, created_at: nil, updated_at: nil>
- >> sa.agent = ag
- => #<Agent id: 1, username: "justme", created_at: "2008-10-15 01:47:36", updated_at: "2008-10-15 01:47:36">
- >> sa.transaction = tr
- => #<Transaction id: 1, trans_number: 2221, created_at: "2008-10-15 01:46:22", updated_at: "2008-10-15 01:46:22">
- >> sa.transaction
- => #<Transaction id: 1, trans_number: 2221, created_at: "2008-10-15 01:46:22", updated_at: "2008-10-15 01:46:22">
- >> sa.save
- => #<Transaction id: 1, trans_number: 2221, created_at: "2008-10-15 01:46:22", updated_at: "2008-10-15 01:46:22">
- >> sa
- => #<SellerAgent id: nil, agent_id: 1, transaction_id: 1, created_at: nil, updated_at: nil>
- >> sa.save
- => #<Transaction id: 1, trans_number: 2221, created_at: "2008-10-15 01:46:22", updated_at: "2008-10-15 01:46:22">
- >> tr.seller_agent
- => nil
- >> tr.agent
- => nil
- >> tr = nil
- => nil
- >> tr = Transaction.find :first
- => #<Transaction id: 1, trans_number: 2221, created_at: "2008-10-15 01:46:22", updated_at: "2008-10-15 01:46:22">
- >> tr.agent
- => nil
- >> tr.seller_agent
- => nil
- >> sa
- => #<SellerAgent id: nil, agent_id: 1, transaction_id: 1, created_at: nil, updated_at: nil>
- >> sa.transaction
- => #<Transaction id: 1, trans_number: 2221, created_at: "2008-10-15 01:46:22", updated_at: "2008-10-15 01:46:22">
- >> sa.agent
- => #<Agent id: 1, username: "justme", created_at: "2008-10-15 01:47:36", updated_at: "2008-10-15 01:47:36">
Add Comment
Please, Sign In to add comment