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.
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 Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
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 <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> 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 > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > > > > >
Take a look at acts_as_list - http://github.com/rails/acts_as_list On Sep 19, 4:25 pm, Fidel Viegas <fidel.vie...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > <R...-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> 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 > > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > >
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. On Sun, Sep 20, 2009 at 1:28 AM, E. Litwin <elitwin-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> wrote:> > Take a look at acts_as_list - http://github.com/rails/acts_as_list > > On Sep 19, 4:25 pm, Fidel Viegas <fidel.vie...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 >> >> <R...-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> 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 >> > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org >> >> > > >
On Sun, Sep 20, 2009 at 1:28 AM, E. Litwin <elitwin-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> wrote:> > Take a look at acts_as_list - http://github.com/rails/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? Thanks in advance, Fidel.
Never mind, Trial and error and managed to install it. Thanks! Fidel. On Thu, Sep 24, 2009 at 6:49 AM, Fidel Viegas <fidel.viegas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Sep 20, 2009 at 1:28 AM, E. Litwin <elitwin-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> wrote: >> >> Take a look at acts_as_list - http://github.com/rails/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? > > Thanks in advance, > > Fidel. >
Fidel Viegas wrote:> On Sun, Sep 20, 2009 at 1:28 AM, E. Litwin <elitwin-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> > wrote: >> >> Take a look at acts_as_list - http://github.com/rails/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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
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 On Thu, Sep 24, 2009 at 12:16 PM, Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Fidel Viegas wrote: >> On Sun, Sep 20, 2009 at 1:28 AM, E. Litwin <elitwin-ur4TIblo6goN+BqQ9rBEUg@public.gmane.org> >> wrote: >>> >>> Take a look at acts_as_list - http://github.com/rails/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, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >
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-KoserBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Yes!!! Thanks!!! Fidel. On Thu, Sep 24, 2009 at 1:28 PM, Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >