I am working on a project where users can create events with limited seats, and register and attend other events.
So far, I have an event, user, and seat model. And have removed the ability for users to create duplicate seats in my Seat model.
class Event < ActiveRecord::Base
validates :title, :from_date, :from_time, :number_of_seats, presence: true
belongs_to :user
has_many :seats
end
class Seat < ActiveRecord::Base
validates :event_id, :user_id, presence: true
validates_uniqueness_of :user_id, :scope => :event_id
belongs_to :event
belongs_to :user
end
Currently, I have a page that lists all the available events. What I am trying to do is create a query of all the events the user is attending, and remove them from the page (or let the user know they are already attending).
I was able to create a page of all the classes a user is attending with this in my seats controller:
def index
@seats = Seat.where(user_id: current_user.id)
end
And this on the page:
<h1 class="page-header">My Classes</h1>
<% @seats.each do |p| %>
<%= link_to Event.find(p.event_id).title, p %>
<% end %>
I have tried solving this problem using Find with conditions: Stack Overflow
It got me close, but no cigar...
How can I create an Active Record Query that returns all Events a user is NOT attending?
You can use .where.not
:
<% Event.where.not(id: @seats.map(&:event_id)).each do |p| %>
<...>
<% end %>