samedi 25 avril 2015

Join the associated object into the belongs_to object


I have an Event model which has many DateEntry because an event can have multiple datetimes.

I have a view where I see all the events with date entries for a specific day. For which I've made this class method in models/event.rb:

def self.by_date(date)
  includes(:date_entries)
  .where(date_entries: { begins_at: date.beginning_of_day..date.end_of_day })
end

And it works fine.

Now on the view I loop over the events I grabbed with that query:

@events.each do |event|
  # whatever

I am looking for a way to use the date from the selected DateEntry in the loop. I think I have to use ActiveRecords joins method, but I have tried in many ways and when I am not getting an error the output is still the same.

For clarifying, I want to do in the loop something like event.selected_date_entry_by_the_by_date_method.begins_at or something like that.


Aucun commentaire:

Enregistrer un commentaire