-2

This is my Rails 4 app: http://wheels2015.herokuapp.com

I want the entries (events) to be listed in chronological order, as opposed to the order in which I entered them into the database (as they are currently).

This question isn't the same as mine, but it appears to be in the general ballpark, so when a user suggested that OP look up the ActiveRecord query methods in the Rails API (http://apidock.com/rails/ActiveRecord/QueryMethods/order) I did.

Do you think this would do what I want it to?

Event.order('date_time')
=> SELECT "events".* FROM "event" ORDER BY event_date_time

This is the portion of my event controller that's responsible for the index page, as well as the event params:

 def event_params
      params.require(:event).permit(:event_name, :location, :description, :event_date_time, :organizer, :category) if params[:event]
    end 

  def index
    @events = Event.all
  end
Community
  • 1
  • 1
Yami Medina
  • 670
  • 10
  • 26
  • 1
    Well, why don't you try it? I don't understand the question. Do you want me to run the code for you and tell you that it works? Because your app could do the same thing for you. – sjagr Jun 26 '15 at 15:31
  • 1
    is `event_date_time` the same as `created_at`? `created_at` is the timestamp when you created the entry. i think you want `Event.order('created_at')` – rob Jun 26 '15 at 15:34
  • @rob i've added a specific field for a date and time that's different. I **don't** want it to sort in order of `created_at`, which I suspect it already does because my latest entry is last even though it will take place before several other entries I made. – Yami Medina Jun 26 '15 at 15:37
  • I'm not sure how this question isn't answered by Rails docs and/or tutorials. – Dave Newton Jun 26 '15 at 15:37
  • `@events = Event.order('event_date_time') => SELECT "events".* FROM "event" ORDER BY event_date_time` gives me a syntax error, so I tried plain old `@events = Event.order('event_date_time')` and it works! thanks, I think just the act of writing out the question has helped. – Yami Medina Jun 26 '15 at 15:39

1 Answers1

1

I think I had some minor trouble understanding the documentation, but this did what I wanted:

events_controller.rb:

  def index
    @events = Event.order('event_date_time')
  end

I swear I didn't ask this question so that I could answer it 5 minutes later; it's more like the action of writing the question helped me think through it? I don't want anyone to make the effort to write an answer, but if someone else is already in the process of working on one i'd upvote theirs instead of my own.

Yami Medina
  • 670
  • 10
  • 26