0

I have a model "Events" and each event contains an array of ids for "Genre" (a different model). When I create an event, I check a bunch of boxes and save the checked genres to the database. That is all working fine.

When I run my loop to display all events, the field genre_ids returns me an array with the id's for each selected genre that needs to be retrieved from the genres table.

I tried suggestions that I found but had no success yet.

Genre model:

class Genre < ActiveRecord::Base
  has_and_belongs_to_many :events
end

Event model:

class Event < ActiveRecord::Base
  belongs_to :user
  has_many :genres
end

Events controller:

class EventsController < ApplicationController

  def index
    @event_all = Event.all
  end

 def get_genres(event_id)
   @event_genre = Event.find(event_id).genre_id
 end
 helper_method :get_genres

end

 -------

And finally the loop:

<div id="index-events-wrap">
   <% @event_all.each do |ea| %>
     <div class="item">
     <div class="event-image">
       image here
     </div>
    <ul>
      <li><%= ea.name %></li>
      <li><%= get_genres(ea.id)  %></li>
      <li><%= ea.venue_name %></li>
      <li><%= ea.venue_address %></li>
      <li><%= ea.venue_postcode %></li>
    </ul>
  </div>
<% end %>
</div>
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Theo Felippe
  • 279
  • 1
  • 2
  • 17

1 Answers1

3

Try with:

@event_all = Event.includes(:genres).all

It will make one more query (to avoid multiple instantiation of the Genre instances) and populate all the associations automatically. So in the view you can:

<li><%= ea.genres %></li>

By the way, you should have has_and_belongs_to_many in both classes, otherwise Rails won't understand it.

class Event < ActiveRecord::Base
  belongs_to :user
  has_and_belongs_to_many :genres # <- this one
end

If needed, add a migration for the join table, see Do I need to manually create a migration for a HABTM join table?

Community
  • 1
  • 1
rewritten
  • 16,280
  • 2
  • 47
  • 50
  • 2
    +1, i was about to answer the same thing. You should also avoid using `.all` (works fine in the beginning, but imagine that with a billion events...). Use `.limit`, or something like [will_paginate](https://github.com/mislav/will_paginate/) / [kaminari](https://github.com/amatsuda/kaminari) – m_x Oct 09 '12 at 20:24
  • I see what you mean about the .all. That is temporary because as soon as i implement it a intended, it won't be .all but based on the event date :) for now i just want to get all on the screen.. thanks a bunch – Theo Felippe Oct 09 '12 at 20:35
  • 1
    I will, but it actually didn't work.. :( I'm getting an error: private method `include' called for # Any ideas? thanks – Theo Felippe Oct 09 '12 at 20:43
  • whops... it's `includes` !! ;) – rewritten Oct 09 '12 at 20:47
  • oh cool! that fixed it. its not working yet because apperantly rails doesn't see the relation between the two. I have added has_and_belongs_to to both but apparently wasn't enough. But i'm guessing that would be a different question? although it'd be great if you shed that last bit of great knowledge towards this :) thanks anyways! – Theo Felippe Oct 09 '12 at 20:51
  • `PG::Error: ERROR: relation "events_genres" does not exist LINE 1: ... ar_association_key_name FROM "genres" INNER JOIN "events_ge... ^ : SELECT "genres".*, "t0"."event_id" AS ar_association_key_name FROM "genres" INNER JOIN "events_genres" "t0" ON "genres"."id" = "t0"."genre_id" WHERE "t0"."event_id" IN ('1', '2')` – Theo Felippe Oct 09 '12 at 20:53
  • It seems rails is "thinking" that in the table genres i should have a field for event_id. But situation is the opposite. In the table events i have a field with genre_id(that contains an array with many ids). any ideas? thanks so much for helping out. – Theo Felippe Oct 09 '12 at 21:04
  • You need a HABTM association, not a serialized array. You can't do much with the serialized array. The error states it clearly: `relation "events_genres" does not exist` so you have to create it. – rewritten Oct 09 '12 at 21:05
  • Not sure how to go about fixing this :( – Theo Felippe Oct 09 '12 at 21:07
  • Please see additional info in original answer. – rewritten Oct 09 '12 at 21:13