Ruby on Rails how to distinguish multiple relationships between same models -


so have following models

user, course, order, line_item 

user (seller) has_many :courses (as instructor, uploading them)

on other hand...

user (buyer): has_many :orders

order: has many :line_items

line_item: belongs_to :course

so want list of courses purchased buyer, can @ point use @user.courses so? how can distinguish between buyer.courses , seller.courses?

any appreciated. thanks!

you need use self-referential association this.

#cousre.rb class course < activerecord::base   belongs_to :sellar, :class_name => 'user', :foreign_key => 'sellar_id' end  #order.rb class order < activerecord::base   belongs_to :buyer, :class_name => 'user', :foreign_key => 'buyer_id' end 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -