Skip to content

Instantly share code, notes, and snippets.

@samflores
Last active June 11, 2016 20:41
Show Gist options
  • Save samflores/a9bee520308c415dc17ed0c894169e81 to your computer and use it in GitHub Desktop.
Save samflores/a9bee520308c415dc17ed0c894169e81 to your computer and use it in GitHub Desktop.
t1 = Thing.create name: 'Book'
t2 = Thing.create name: 'Shirt'
t3 = Thing.create name: 'Game'
p1 = Person.create name: 'John'
p2 = Person.create name: 'Jane'
p3 = Person.create name: 'Mary'
Ownership.create owner: p1, thing: t1 # John owns a Book
Ownership.create owner: p2, thing: t2 # Jane owns a Shirt
Ownership.create owner: p3, thing: t3 # Mary owns a Game
Wish.create wisher: p2, thing: t3 # Jane wishes a Game
Wish.create wisher: p2, thing: t1 # Jane wishes a Book
Wish.create wisher: p1, thing: t2 # John wishes a Shirt
p2.give_to(p1) # returns Shirt since John wishes one and Jane has one
p1.receive_from(p2) # returns Shirt since John wishes one and Jane has one
# new methods
p1.possible_to_give # returns Jane since she wants a Book and John has a Book
p1.possible_to_receive # returns Jane since she has a Shirt and John wants a Shirt
p1.possible_to_trade # returnd Jane since she has an item John wants ans wants an item John has
# rails g ownership thing:references owner:references
class Ownership < ActiveRecord::Base
belongs_to :thing
belongs_to :owner, class_name: 'Person'
attr_accessible :thing, :owner
end
# rails g model person name:thing
class Person < ActiveRecord::Base
attr_accessible :name
has_many :ownerships, foreign_key: :owner_id
has_many :owned_things, through: :ownerships, source: :thing
has_many :wishes, foreign_key: :wisher_id
has_many :wished_things, through: :wishes, source: :thing
# returns all people that OWNS some item
scope :owns, ->(things_ids) {
joins(:owned_things)
.where(things: { id: things_ids })
}
# return all people that WANTS some item
scope :wants, ->(things_ids) {
joins(:wished_things)
.where(things: { id: things_ids })
}
def give_to(person)
owned_things.where(id: person.wished_thing_ids)
end
def receive_from(person)
wished_things.where(id: person.owned_thing_ids)
end
# returns all people who WANTS the items that this person OWNS
def possible_to_give
Person.wants(owned_things)
end
# returns all people who OWNS the items that this person WANTS
def possible_to_receive
Person.owns(wished_things)
end
# returns the intersection of the above methods, i.e.
# people who WANT something this person OWNS *and* OWNS something this person WANTS
def possible_to_trade
possible_to_give & possible_to_receive
end
end
# rails g model thing name:string
class Thing < ActiveRecord::Base
attr_accessible :name
has_many :ownerships, foreign_key: :thing_id
has_many :owners, through: :ownerships, source: :owner
has_many :wishes, foreign_key: :wish_id
has_many :wishers, through: :wishes, source: :wisher
end
# rails g model wish thing:references owner:references
class Wish < ActiveRecord::Base
belongs_to :thing
belongs_to :wisher, class_name: 'Person'
attr_accessible :thing, :wisher
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment