Hi, I''m trying to set up a toy invoicing app. (a) Invoice made up of line items; (b) Line items looking up data from; I think I can see how to do (a) starting with> class Invoice < ActiveRecord::Base > has_many :line_items > ...plus SQL of:> create table invoices ( > id int not null auto_increment, > customer varchar(100) not null, > primary key (id) > );and> class LineItem < ActiveRecord::Base > belongs_to :invoice > ...plus SQL of:> create table line_items ( > id int not null auto_increment, > product_id int not null, > order_id int not null, > quantity int not null default 0, > unit_price decimal(10,2) not null, > constraint fk_items_order foreign key (order_id) references > orders(id), > primary key (id) > );I would like the user to be able to select a product from a pop-up - so I realise I need to use "collection_select" - maybe something like> @products = Product.find(:all, :order => "code").map {|p| [p.name, > p.id] } > select(:product, :name, @products)given SQL of> create table products ( > id int not null auto_increment, > code varchar(10) not null, > title varchar(100) not null, > description text not null, > price decimal(10,2) not null, > primary key (id) > );but I have two problems. One, do I need a foreign key in the line_items table for product (as suggested in ADWDR p226), and if so WHY? (a product doesn''t seem to ''belong_to'' line_items, and the saving associated records thing has me worried). Two, where do I put this piece of code - as a method in LineItemsController? Any help greatfully received. R [As an aside, I''m having a complete nightmare getting my head around Rails. There are lots of place to find out how to do a small, initial part of x (and lots of intra-referencing between them), but very little help on the why. Generally, I find that the ''why'' is important in figuring out where to join all the little bits and pieces together (especially, since the fact that rails is ''spread out'' across a bunch of files makes it harder for idiots like myself to see the what''s happening). In my defence, I may be an idiot, but not actually a moron... If someone could explain it in few enough syallables, I''d be delighted to come up with a more graphical explanation of how it all hangs together.]
Richard Dyce wrote:> Hi, > > I''m trying to set up a toy invoicing app.Me too [1]>> class LineItem < ActiveRecord::Base >> belongs_to :invoice >> ... > > plus SQL of: > >> create table line_items ( >> id int not null auto_increment, >> product_id int not null, >> order_id int not null, >> quantity int not null default 0, >> unit_price decimal(10,2) not null, >> constraint fk_items_order foreign key (order_id) references >> orders(id), >> primary key (id) >> );OK> > but I have two problems. One, do I need a foreign key in the line_items > table for product (as suggested in ADWDR p226), and if so WHY? (a > product doesn''t seem to ''belong_to'' line_items, and the saving > associated records thing has me worried). Two, where do I put this > piece of code - as a method in LineItemsController? >with the table above "a LineItem will ''belong to'' a Product" (you can read it also like "a LineItem ''references'' a Product") thus you''ll be able to say that "a Product ''has many'' LineItems pointing to it". This is useful also to be able to easily present some kind of "selling numbers" from Product (e.g. how many times it has been sold: just sum all the Product''s line_items quantity, multiply that per the ) HTH Luca [1] seemed a nice way to do something concrete with ROR and at the same time make up something useful
Richard Dyce wrote:> Hi, > > I''m trying to set up a toy invoicing app. > > (a) Invoice made up of line items; > (b) Line items looking up data from; > > I think I can see how to do (a) starting with > >> class Invoice < ActiveRecord::Base >> has_many :line_items >> ... > > > plus SQL of: > >> create table invoices ( >> id int not null auto_increment, >> customer varchar(100) not null, >> primary key (id) >> ); > > > and > >> class LineItem < ActiveRecord::Base >> belongs_to :invoice >> ... > > > plus SQL of: > >> create table line_items ( >> id int not null auto_increment, >> product_id int not null, >> order_id int not null, >> quantity int not null default 0, >> unit_price decimal(10,2) not null, >> constraint fk_items_order foreign key (order_id) references >> orders(id), >> primary key (id) >> ); > > > I would like the user to be able to select a product from a pop-up - so > I realise I need to use "collection_select" - maybe something like > >> @products = Product.find(:all, :order => "code").map {|p| [p.name, >> p.id] } >> select(:product, :name, @products) > > > given SQL of > >> create table products ( >> id int not null auto_increment, >> code varchar(10) not null, >> title varchar(100) not null, >> description text not null, >> price decimal(10,2) not null, >> primary key (id) >> ); > > > but I have two problems. One, do I need a foreign key in the line_items > table for product (as suggested in ADWDR p226), and if so WHY? (a > product doesn''t seem to ''belong_to'' line_items, and the saving > associated records thing has me worried). Two, where do I put this > piece of code - as a method in LineItemsController? >It looks like you already have a foreign key in your line_items table (product_id) it''s just that you haven''t enforced it via a constraint. However, if you set the necessary AR relationships in your models you don''t actually need to enforce it in the DB anyway. class Product has_many :line_items end class LineItem belongs_to :product end The collection select you will want should look something like this. <%= collection_select :line_item, :product_id, @products, :id, :title %> Where @products is populated in your controller with something like @products = Product.find :all Shout if you need more direction on where all of this should go. Chris> Any help greatfully received. > > R > > [As an aside, I''m having a complete nightmare getting my head around > Rails. There are lots of place to find out how to do a small, initial > part of x (and lots of intra-referencing between them), but very little > help on the why. Generally, I find that the ''why'' is important in > figuring out where to join all the little bits and pieces together > (especially, since the fact that rails is ''spread out'' across a bunch > of files makes it harder for idiots like myself to see the what''s > happening). In my defence, I may be an idiot, but not actually a > moron... If someone could explain it in few enough syallables, I''d be > delighted to come up with a more graphical explanation of how it all > hangs together.] > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >