1

I have 3 invoices that belong to the same shipment. The first invoice created is always the main invoice of the shipment.

Invoice 
id   shipment_id
21   55 # This is the main invoice
88   55
93   55

If I perform the following query

s = Shipment.find(55)
s.invoices[0] # 21
s.invoices[1] # 88
s.invoices[2] # 93

So I'm guessing the order of the child elements is determined by their id's. Am I right? Or is there something more to it?

I'm asking because I need to be certain of the order of the child elements to make sure one of my methods will always work.

def am_i_the_main_invoice?
  if self.shipment.invoices[0].id == self.id
    true
  else
    false
  end
end

4 Answers 4

2

The order is random unless you explicitly set it. While your database will probably return the elements in the same order right now, it can be a any other order if you change anything (even seemingly unrelated stuff). So again: don't trust the order of elements retrieved from a relational database unless you have explicitly set the order in your query.

In your Shipment class where you probably have setup a has_many relation, you can add something like :order => "id ASC" to always enforce the order by id.

1

I would add an association extension for the sake of readability

class Shipment < ActiveRecord::Base
  has_many :invoices do
    def main
      @main_invoice ||=  first(:order => "id ASC")
    end

    def main?(id)
      main.try(:id) == id
    end
  end
end

Now you can get the main invoice as follows:

shipment.invoices.main # returns main invoice
shipment.invoices.main?(23) # checks if the invoice is a main invoice

This way, you are cleanly exposing the concept of main invoice.

2
  • I had to modify the query because I kept on getting errors, so this is what's inside the main_invoice method: self.invoices(:order => 'id ASC').first. I like this approach because it doesn't require changes to the database.
    – leonel
    Aug 22, 2012 at 17:19
  • What was the error? Also, I don't think invoices(:order => 'id ASC') modifies the sort order. The association method takes boolean parameter to reload the association array. I would use self.order("id ASC").first. I have used the code I have given and it has worked for me before. Aug 22, 2012 at 18:14
1

Instead of depending on an ordered query, you could add a flag to the invoices table:

# in migration
add_column :invoices, :is_primary, :boolean, {null: false, default: false}

# if your rdbms supports partial indexes, you can enforce it
execute 'create unique index uniq_single_primary on invoices (shipment_id) where is_primary = true'

# then query is easy ..
def am_i_the_main_invoice?
  self.is_primary?
end
1

The order returned usually depends on the SQL Database you're using. Usually it is based on the primary key, but as Holger Just said, don't trust it cause it can change.

You can work around this in 2 ways:

  1. Add a scope in your Invoice model, which you can call on before you look through your collection, e.g. scope :by_shipment, order("id ASC")
  2. Add a default_scope so that it will always be retrieved in that order, e.g. default_scope order("id ASC")

Hope this helps

2
  • Only in certain databases (e.g MySQL) you often get data ordered by primar key when not specifying order. However, even MySQL returns unordered data sometimes (e.g after compaction). Other databases like Postgres return out ordered data much more frequent. Generally (according to the various SQL standards) if the order is not specified, it can be random. Aug 23, 2012 at 7:23
  • @HolgerJust Personally I always set an order, unless I don't care, because you're right and even MySQL brings them out unordered. SQLite seems to bring them out in order too from what I've seen. But rather be safe than sorry, right ;) Aug 23, 2012 at 7:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.