Advertisement
dariys

Untitled

Jun 3rd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.17 KB | None | 0 0
  1. class User < ActiveRecord::Base
  2.   has_many :claimant_disputes, class_name: 'Dispute', :foreign_key => :claimant_id
  3.   has_many :indicted_disputes, class_name: 'Dispute', :foreign_key => :indicted_id
  4. end
  5.  
  6. class Dispute < ActiveRecord::Base
  7.   belongs_to :claimant, class_name: 'User', :foreign_key => :claimant_id
  8.   belongs_to :indicted, class_name: 'User', :foreign_key => :indicted_id
  9. end
  10.  
  11. #соответственно юзаеш как обычно
  12. dispute = Dispute.create(:name=>'Dispute1')
  13. cl_user = User.create(:name=>'Claimant user')
  14. in_user = User.create(:name=>'Indicted user')
  15.  
  16. #ну и связывание работает в обе стороны
  17. dispute.claimant = cl_user
  18. in_user.indicted_disputes << dispute
  19.  
  20. #в результате
  21. dispute.claimant #первый юзер
  22. dispute.indicted #второй юзер
  23. in_user.indicted_disputes #для второго юзера все его индиктед диспуты в данном случае один шо мы создали в списке
  24. in_user.claimant_disputed #для второго юзера все его клаймант диспуты в данном случае пустой список
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement