title: basic rails model has_many and belongs_to with group by date
body: <p>I'm a rails newbie so pardon if this is a basic question. I'm setting up some models and the relationships should be pretty basic, but I'm a little stuck in the best way to set everything up.</p>
  
  <p>I'm trying to display a list of seminars that have different times. Here are my two tables:</p>
  
  <pre><code>create_table "seminars", :force =&gt; true do |t|
    t.string   "name"
    t.datetime "created_at", :null =&gt; false
    t.datetime "updated_at", :null =&gt; false
  end
  
  create_table "seminar_times", :force =&gt; true do |t|
    t.datetime "start_date"
    t.datetime "end_date"
    t.integer  "seminar_id"
    t.datetime "created_at", :null =&gt; false
    t.datetime "updated_at", :null =&gt; false
  end
  </code></pre>
  
  <p>I'd like to display my seminars at localhost:3000/seminars like so:</p>
  
  <pre><code>Intro To Rails      Aug 1   3-5pm and 6-8pm
  How To Juggle       Aug 10  6-8pm
  Stackoverflow Rocks Aug 13  2-5pm or 6-8pm
  </code></pre>
  
  <p>One seminar can have many different seminar times (which is why i put them into two separate tables). To do this normally with straight SQL I'd join the tables, order by start_date, then group by DAY and by seminar id.</p>
  
  <p>How would I do this the "rails way"?</p>
  
  <p>Here's what I've done so far, but it doesn't seem like the right way to go.</p>
  
  <p>Seminar model:</p>
  
  <pre><code>class Seminar &lt; ActiveRecord::Base
    attr_accessible :name
    has_many :seminar_times, :dependent =&gt; :destroy
    validates :name, :presence =&gt; true
  end
  </code></pre>
  
  <p>SeminarTime model:</p>
  
  <pre><code>class SeminarTime &lt; ActiveRecord::Base
    attr_accessible :end_date, :seminar_id, :start_date
    belongs_to :seminar, :foreign_key =&gt; 'seminar_id'
    default_scope :order =&gt; 'start_date'
  end
  </code></pre>
  
  <p>seminars_controller:</p>
  
  <pre><code>def index
    @seminars = SeminarTime.group('date(start_date), seminar_id') #THIS LINE ESPECIALLY SEEMS WRONG TO ME
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @seminars }
    end
  end
  </code></pre>
  
  <p>seminar_times_controller:</p>
  
  <pre><code>def index
    @seminar_times = SeminarTime.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @seminar_times }
    end
  end
  </code></pre>
  
  <p>routes.rb</p>
  
  <pre><code>resources :seminars 
  resources :seminar_times
  root to: 'static_pages#home'
  match '/seminars',    to: 'seminars#index'
  </code></pre>
  
  <p>views/seminars/index.html.erb:</p>
  
  <pre><code>#this also needs some work. i'm not sure how to loop into that group by statement
  &lt;% @seminars.each do |seminar| %&gt;
    &lt;%= seminar.seminar.name %&gt;
    &lt;%= seminar.start_date %&gt;
    &lt;%= seminar.end_date %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I've tried also getting everything through the seminar_times_controller and then routing /seminars to:
      match '/seminars',    to: 'seminar_times#index'
  but that also seems kinda off to me.</p>
  
  <p>Hopefully I'm being clear enough. Any guidance is much appreciated. Thanks!</p>
  
  <hr>
  
  <p>My app evolved some since I asked this question. I ended up adding some categories, so I made a shared module so I could use my sorting code throughout the app. The sort and grouping looks like this:</p>
  
  <pre><code>module Shared
    def self.category_group_by_day(categories_array)
      seminars = Seminar.joins(:seminar_times, :categories).
        where(:categories =&gt; {:name =&gt; categories_array}).
        where("date(start_time) &gt; ?", DateTime.now)
  
      seminars.sort_by! { |s| s.start_date }
      seminar_days = seminars.group_by { |s| [s.start_date.beginning_of_day, s.course_id] }
    end
  end
  </code></pre>
  
  <p>I call it in my controller like this:</p>
  
  <pre><code>@seminar_days = Shared::category_group_by_day(['Free Seminar', 'Online Seminar'])
  </code></pre>
  
  <p>And then in my view:</p>
  
  <pre><code>&lt;% @seminar_days.keys.sort.each do |day| %&gt; 
   &lt;%= @seminar_days[day].first.course_name %&gt;
    &lt;%= day.to_s.to_date.strftime("%b %e") %&gt; &lt;%= day.to_s.to_date.strftime("%a") %&gt;
      &lt;% for seminar in @seminar_days[day] %&gt;
        &lt;div&gt;
          &lt;%= seminar.start_date.strftime("%l").strip %&gt;—&lt;%= seminar.end_date.strftime("%l").strip %&gt; &lt;%= seminar.end_date.strftime("%p") %&gt;
        &lt;/div&gt;
      &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>