Ordered Sets in Ruby

Hi all,

I am implementing a simple invoice system where I have invoiceLines, and where I want to keep the order of the lines. I have tried to google around but have not found any sample on this.

Can someone give me a pointer to something that uses a set where the order is kept?

Thanks in advance.

Regards,

Fidel.

Well, that would be an Array. Or in the likely case that your data is accessed with ActiveRecord models, an AssociationProxy that will behave mostly like an Array. You will probably find something like the following in any reasonable discussion about the has_many/belongs_to associations. If you don't specify a default order for the association, then you'll get the invoice_lines back in whatever order the database prefers (often the order in which the database records were created).

## Models class Invoice < ActiveRecord::Base    has_many :invoice_lines, :order => 'line_number' end class InvoiceLine < ActiveRecord::Base    belongs_to :invoice end

## migration (schema) create_table :invoices do |t|    t.integer :number    t.date :inv_date    t.date :due_date    # etc. end

create_table :invoice_lines do |t|    t.references :invoice    # equiv. to t.integer :invoice_id    t.integer :line_number    t.string :sku    t.integer :quantity    t.decimal :unit_price, :precision => 8, :scale => 2    # etc. end

## use in a controller

@invoice = Invoice.find_by_number(params[:invoice][:number])

@invoice.invoice_lines #=> an array-like set of InvoiceLine records

-Rob

Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

Hi Rob,

Thanks for the reply. I have managed to get this code done. What I wanted really was how to save and update.

For instance, I want to know how to create the instance of the invoice, and the invoice lines. When updating the invoice, its line items with the respective order should be also updated in the db.

If I remove one item from the list, the order of each item should be adjusted.

Let's say I have the following invoiceitems:

item, qty, price, order 1, 3, 12.0, 1 6, 1, 23.0, 2 3, 2, 11.0, 3

if I remove item with order 2, I should get the following:

1, 3, 12.0, 1 3, 2, 11.0, 2

What I am after is a view + controller solution.

Thanks in advance.

Regards,

Fidel.

Take a look at acts_as_list - http://github.com/rails/acts_as_list

Hi Litwin,

Thanks a lot for the link.

I will give it a try, but from what I saw that is exactly what I am looking for.

Regards,

Fidel.

Hi Litwin,

I have cloned the git file to my hard drive, and tried running the init.rb, but that has given me this error:

init.rb:3: uninitialized constant ActiveRecord::Base (NameError)

How do I install it?

Thanks in advance,

Fidel.

Never mind,

Trial and error and managed to install it.

Thanks!

Fidel.

Fidel Viegas wrote:

Take a look at acts_as_list - GitHub - rails/acts_as_list: NOTICE: official repository moved to https://github.com/swanandp/acts_as_list

Hi Litwin,

I have cloned the git file to my hard drive, and tried running the init.rb, but that has given me this error:

init.rb:3: uninitialized constant ActiveRecord::Base (NameError)

How do I install it?

Use script/plugin install.

Thanks in advance,

Fidel.

Best,

Hi Marnen,

Thanks for the reply. I found that out through trial and error. I first tried script/plugin install act_as_list, but that didn't work. So, I installed from github directly.

Thanks once again.

Regards,

Fidel

Fidel Viegas wrote:

Hi Marnen,

Thanks for the reply. I found that out through trial and error. I first tried script/plugin install act_as_list, but that didn't work. So, I installed from github directly.

Right. Script/plugin install takes a Git (or SVN) repository URL. It pulls the code from the repo, installs it in the appropriate place, and then runs init.rb. It's magic!

Thanks once again.

Regards,

Fidel

On Thu, Sep 24, 2009 at 12:16 PM, Marnen Laibow-Koser

Best,

Yes!!!

Thanks!!!

Fidel.