0

I have a a .each Do loop in rails which works fine on MySQL but when I upload it to Heroku (and therefore Postgres) it doesn't work correctly.

I want the items to be ordered by time, preferably the most recent at the top. Here's the first bit of code I had:

<% @envelope.transactions('created_at DESC').reverse.each do |transaction|%>

When I noticed the problem with Postgresql I looked around and switched the code to the following, it still seems to work on mysql but it still doesn't work on Heroku:

<% @envelope.transactions(:order=>'DATE(created_at) DESC').reverse.each do |transaction|%>

anyway, I'm at a loss, mostly because I know nothing about databases. how do I get Postgres to respond to my request in Rails 4 to have the most recent ones at the top?

Thanks!

UPDATE:

Here is some sample output as an example of what I'm talking about:

I input 3 things and it put them in correctly:

First Image

After Edit, order changed

You can go try it yourself at: http://couplesbudget.herokuapp.com/envelopes/6/edit

username: [email protected] password: test123456

Update2

I recreated the above situation on my dev pc (with mysql) and here is what I got as far as details go: first stuff

as you can see envelope 1 is created at 3:25:06 as you can see envelope 2 is created at 3:25:11 as you can see envelope 3 is created at 3:25:19

Here's the data from the edit: enter image description here

then after the change:

as you can see envelope 1 is created at 3:25:06 as you can see envelope 2 is created at 3:25:11 as you can see envelope 3 is created at 3:25:19

So its not changed. why then does postgresql not order the instances correctly?

3
  • What is @envelope? What is @envelope.transactions? Commented Sep 12, 2014 at 2:08
  • Could you post sample output from PostgreSQL where the data is not ordered as expected? Commented Sep 12, 2014 at 2:19
  • But what are the timestamp values? What is @envelope? What is this transactions method? Commented Sep 12, 2014 at 3:03

1 Answer 1

1

Presumably you have models something like this:

class Transaction < ActiveRecord::Base
  belongs_to :envelope
end
class Envelope < ActiveRecord::Base
  has_many :transactions
end

So your @envelope is an Envelope and it gets its transactions method through the has_many :transactions association. That means that the transactions method doesn't care what arguments you give it and these do exactly the same thing:

@envelope.transactions('created_at DESC')
@envelope.transactions(:order=>'DATE(created_at) DESC')
@envelope.transactions(:where_is_pancakes_house?)
@envelope.transactions

and that thing doesn't involve sending an ORDER BY to the database. If you don't tell the database what order you want things in then you shouldn't expect them to come out in any particular order; the results you're seeing with your local MySQL are either accidental or a MySQL implementation detail.

If you want to get your data in a particular order, then say so:

@envelope.transactions.order('created_at desc').reverse

or just order them in ascending order and skip the reverse:

@envelope.transactions.order(:created_at).reverse

BTW, developing on top of one database and deploying on another is a recipe for disaster. ActiveRecord will not provide you with database portability regardless of the marketing claims.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! that helps a lot, out of my entire project, this is the line than I understood the least about, but now seeing that I wasn't actauly passing it the arguments that I thought, everything is clear thanks!
A handy trick is to look at the SQL by tacking a to_sql call on the end (envelope.transactions.to_sql for example) in the console. That assumes, of course, that you know enough SQL to know what you're looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.