Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

rails 3 group_by for simple active record groupings

Posted on 2010-11-21
3
Medium Priority
?
1,086 Views
Last Modified: 2012-08-14
so i have fixtures that have different seasons, i want to group and display the fixtures by season. On the fixtures index page i want the current season displayed and then link to old seasons. I was thinks some kind of group_by method?
class FixturesController < ApplicationController
 
  def index
    @fixtures = Fixture.all
  end
end

##view
<h1>Listing fixtures</h1>

<% @fixtures.each do |fixture| %>
  <%= fixture.season.title %></td>
  <td><%= fixture.event.title %></td>
  <td><%= fixture.home_team.name %></td>
  <td><%= fixture.away_team.name %></td>
  <td><%= fixture.date %></td>

class Season < ActiveRecord::Base
  has_many :fixtures
end

class Fixture < ActiveRecord::Base
  belongs_to :season
  belongs_to :event
  belongs_to :home_team, :class_name => 'Team'
  belongs_to :away_team, :class_name => 'Team'
end

Open in new window

0
Comment
Question by:depassion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:lusfernandos
ID: 34425381
I don't know how you are getting the @fixtures in your index action ... anyway I believe you just need to order by season
something like:

@fixtures = Fixture.all(:order=>"season_id ASC")
0
 

Accepted Solution

by:
depassion earned 0 total points
ID: 34426260
i"ve solved it by creating a get_season method which works out the current season.
#application_controller
before_filter :get_season

def get_season
    @current_season = Season.where(["start_date < ? AND end_date > ?", Date.today, Date.today]).first
  end


#fixtures_view
<% @current_season.fixtures.each do |fixture| %>
...
<%end%>

Open in new window

0
 

Author Closing Comment

by:depassion
ID: 34447401
took too long for a solution from the community so was forced to pay to get help
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question