1

This question is related to other: Many-to-many relationships with Ruby, Redis, and Ohm

I have a Model with a collection and I would like to look for an id. How can I do this?

Models

class User < Ohm::Model
    attribute :name
end

class Event < Ohm::Model
    attribute :title
    collection :attendees, :User
end

Usage

@fran = User.create(name: "Fran")
@event = Event.create(title: "Party in Las Vegas")
@event.attendees.add(@fran)

Event.find(attendees: @fran)
=> Ohm::IndexNotFound exception!

What I would like is to be able to ask by the Users which attending of a Event and what are the Events by an User.

Community
  • 1
  • 1
Fran b
  • 3,016
  • 6
  • 38
  • 65

2 Answers2

1

I'm not familiar with Ohm but I think you only need to add the index

class Event < Ohm::Model
  attribute :title
  collection :attendees, :User
  index :attendees
end

Then you should be able to do

Event.find(attendees: @fran)

OR

Event.find(attendees: @fran.id)
Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
1

You want a set of all users attending an event, and you also want a set of all events attended by a user. The simplest way would be to create a model Attendee that references both a user and an event:

class Attendee < Ohm::Model
  reference :user, :User
  reference :event, :Event
end

u = User.create(name: "foo")
e = Event.create(name: "bar")

a = Attendee.create(user: u, event: e)

Attendee.find(user_id: u.id).include?(a)  #=> true
Attendee.find(event_id: e.id).include?(a) #=> true

Then if you want all users that attended an event:

Attendee.find(event_id: e.id).map(&:user)

Or all the events attended by a user:

Attendee.find(user_id: e.id).map(&:event)

You can create methods in User and Event as shortcuts for those finders:

class User < Ohm::Model
  attribute :name

  def events
    Attendee.find(user_id: id).map(&:event)
  end
end

class Event < Ohm::Model
  attribute :name

  def visitors
    Attendee.find(event_id: id).map(&:user)
  end
end

Then:

u.events.include?(e)   #=> true
e.visitors.include?(u) #=> true

Let me know if it works for your use case.

soveran
  • 872
  • 6
  • 8
  • So ... with Ohm I have to manage Many to Many relationships by hand. I thought there was something as has_and_belongs_to_many like in ActiveRecord. But this works anyway. – Fran b Jan 07 '15 at 16:29