postgresql - Associates the same model twice through different name -


i trying implement lost , found database. have 2 model, user , item. user can lost an item , found item. , item can have user found , user lost it. want able reference the same model through different name, e.g.

user.found_items, user.lost_items, item.founder, item.losser 

right able do:

user.founds, user.losts , user.items return items losts

class user < activerecord::base   has_many :founds   has_many :items, through: :founds    has_many :losts   has_many :items, through: :losts end  class lost < activerecord::base   belongs_to :user   belongs_to :item end  class found < activerecord::base   belongs_to :user   belongs_to :item end  class item < activerecord::base   has_one :found   has_one :user, through: :found    has_one :lost   has_one :user, through: :lost end 

i pretty similar rename them clarity , add methods functions wanted.

class user < activerecord::base    has_many :found_items   has_many :items, through: :found_item    has_many :lost_items   has_many :items, through: :lost_item    def items_found     self.found_items.map {|i| i.item.name }   end    def items_lost     self.lost_items.map {|i| i.item.name }   end  end  class lostitem < activerecord::base   belongs_to :user   belongs_to :item end  class founditem < activerecord::base   belongs_to :user   belongs_to :item end  class item < activerecord::base   has_one :found_item   has_one :user, through: :found_item    has_one :lost_item   has_one :user, through: :lost_item    def finder     self.found_item.user.name   end    def loser     self.lost_item.user.name   end end 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -