Take a look at acts_as_list - http://github.com/rails/acts_as_list
On Sep 19, 4:25 pm, Fidel Viegas <[email protected]> wrote: > 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. > > On Sat, Sep 19, 2009 at 11:59 PM, Rob Biedenharn > > <[email protected]> wrote: > > > On Sep 19, 2009, at 5:35 PM, Fidel Viegas wrote: > >> 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 > > [email protected] > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

