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?
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
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.
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 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