So this whole many-to-many thing has me really confused. I get how to set it up but I can''t seem to find anywhere that really explains the best way to use it in your controller. *What I have:* Products, Orders, Orders_Products The Products and Orders table has the standard stuff (Id, name, timestamps, etc) The Orders_Products table has the two id columns (order_id, product_id) and a quantity column. *What I need to do:* Now when I save my "cart" how do you save a new order and each product with its quantity? This is my first app in rails so the more explanation the better. Thanks in advance for all the help! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Feb 28, 2012, at 11:42 PM, Brandon Might wrote:> So this whole many-to-many thing has me really confused. I get how to set it up but I can''t seem to find anywhere that really explains the best way to use it in your controller. > > What I have: > > Products, Orders, Orders_Products > > The Products and Orders table has the standard stuff (Id, name, timestamps, etc) > > The Orders_Products table has the two id columns (order_id, product_id) and a quantity column.What do your models look like, exactly? What do your has_many and belongs_to methods read as?> > What I need to do: > > Now when I save my "cart" how do you save a new order and each product with its quantity?If you have set up your models correctly to explain the relationship, then all you need to do is get a handle to your cart: @cart = Order.find(session[:cart_id]) #for example and then add to its products collection @cart.products.create(params[:products]) #assuming you have a form for the products When you save @cart, the relationship will be saved to the database, when you find @cart a second time, the @cart.products will be available to work with. Walter> > This is my first app in rails so the more explanation the better. > > Thanks in advance for all the help! > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i''m very new to this too so this may be wrong (shameless disclaimer) but.... you mentioned quantity in your question but there''s no reference to quantity in your description of the models. I would think that you''d also need a quantity field in the Orders_Products model/table to support that and from my reading you would need to use has_many :through. I''m personally battling with learning this now also so i could be wrong and don''t have enough knowledge to give you the details on it all. good luck! :) -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I actually did mention quantity in my model: "The Orders_Products table has the two id columns (order_id, product_id) and a quantity column". On Thursday, March 1, 2012 12:51:47 PM UTC-5, Max wrote:> > i''m very new to this too so this may be wrong (shameless disclaimer) > > but.... > > you mentioned quantity in your question but there''s no reference to > quantity in your description of the models. I would think that you''d > also need a quantity field in the Orders_Products model/table to > support that and from my reading you would need to use > has_many :through. > > I''m personally battling with learning this now also so i could be > wrong and don''t have enough knowledge to give you the details on it > all. good luck! :)-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/f4knCYF25z8J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
My models: class Product < ActiveRecord::Base has_many :Orders_Products has_many :Orders, through: :Orders_Products end class OrdersProduct < ActiveRecord::Base belongs_to: Order belongs_to: Product end class Order < ActiveRecord::Base has_many :Order_Products has_many :Products, through: :Orders_Products end Now my next question is when I do @cart.products.create(session[:products]), if I have more fields in the session like current price (I just added that to the database) and quantity will it save it to the Orders_Products table or will it just ignore those fields? Thanks for all the help, -Brandon On Thursday, March 1, 2012 8:48:16 AM UTC-5, Walter Lee Davis wrote:> > > On Feb 28, 2012, at 11:42 PM, Brandon Might wrote: > > > So this whole many-to-many thing has me really confused. I get how to > set it up but I can''t seem to find anywhere that really explains the best > way to use it in your controller. > > > > What I have: > > > > Products, Orders, Orders_Products > > > > The Products and Orders table has the standard stuff (Id, name, > timestamps, etc) > > > > The Orders_Products table has the two id columns (order_id, product_id) > and a quantity column. > > What do your models look like, exactly? What do your has_many and > belongs_to methods read as? > > > > What I need to do: > > > > Now when I save my "cart" how do you save a new order and each product > with its quantity? > > If you have set up your models correctly to explain the relationship, then > all you need to do is get a handle to your cart: > > @cart = Order.find(session[:cart_id]) #for example > > and then add to its products collection > > @cart.products.create(params[:products]) #assuming you have a form for the > products > > When you save @cart, the relationship will be saved to the database, when > you find @cart a second time, the @cart.products will be available to work > with. > > Walter > > > > This is my first app in rails so the more explanation the better. > > > > Thanks in advance for all the help! > > > > > > -- > > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >On Thursday, March 1, 2012 8:48:16 AM UTC-5, Walter Lee Davis wrote:> > > On Feb 28, 2012, at 11:42 PM, Brandon Might wrote: > > > So this whole many-to-many thing has me really confused. I get how to > set it up but I can''t seem to find anywhere that really explains the best > way to use it in your controller. > > > > What I have: > > > > Products, Orders, Orders_Products > > > > The Products and Orders table has the standard stuff (Id, name, > timestamps, etc) > > > > The Orders_Products table has the two id columns (order_id, product_id) > and a quantity column. > > What do your models look like, exactly? What do your has_many and > belongs_to methods read as? > > > > What I need to do: > > > > Now when I save my "cart" how do you save a new order and each product > with its quantity? > > If you have set up your models correctly to explain the relationship, then > all you need to do is get a handle to your cart: > > @cart = Order.find(session[:cart_id]) #for example > > and then add to its products collection > > @cart.products.create(params[:products]) #assuming you have a form for the > products > > When you save @cart, the relationship will be saved to the database, when > you find @cart a second time, the @cart.products will be available to work > with. > > Walter > > > > This is my first app in rails so the more explanation the better. > > > > Thanks in advance for all the help! > > > > > > -- > > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Su48S3N12NYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Correction: class OrdersProduct < ActiveRecord::Base belongs_to :Order belongs_to :Product end On Thursday, March 1, 2012 6:03:37 PM UTC-5, Brandon Might wrote:> > My models: > > class Product < ActiveRecord::Base > > > has_many :Orders_Products > has_many :Orders, through: :Orders_Products > end > > class OrdersProduct < ActiveRecord::Base > > > belongs_to: Order > belongs_to: Product > end > > class Order < ActiveRecord::Base > has_many :Order_Products > has_many :Products, through: :Orders_Products > > > end > > Now my next question is when I do > @cart.products.create(session[:products]), if I have more fields in the > session like current price (I just added that to the database) and quantity > will it save it to the Orders_Products table or will it just ignore those > fields? > > Thanks for all the help, > -Brandon > > On Thursday, March 1, 2012 8:48:16 AM UTC-5, Walter Lee Davis wrote: >> >> >> On Feb 28, 2012, at 11:42 PM, Brandon Might wrote: >> >> > So this whole many-to-many thing has me really confused. I get how to >> set it up but I can''t seem to find anywhere that really explains the best >> way to use it in your controller. >> > >> > What I have: >> > >> > Products, Orders, Orders_Products >> > >> > The Products and Orders table has the standard stuff (Id, name, >> timestamps, etc) >> > >> > The Orders_Products table has the two id columns (order_id, product_id) >> and a quantity column. >> >> What do your models look like, exactly? What do your has_many and >> belongs_to methods read as? >> > >> > What I need to do: >> > >> > Now when I save my "cart" how do you save a new order and each product >> with its quantity? >> >> If you have set up your models correctly to explain the relationship, >> then all you need to do is get a handle to your cart: >> >> @cart = Order.find(session[:cart_id]) #for example >> >> and then add to its products collection >> >> @cart.products.create(params[:products]) #assuming you have a form for >> the products >> >> When you save @cart, the relationship will be saved to the database, when >> you find @cart a second time, the @cart.products will be available to work >> with. >> >> Walter >> > >> > This is my first app in rails so the more explanation the better. >> > >> > Thanks in advance for all the help! >> > >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups "Ruby on Rails: Talk" group. >> > To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. >> > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > On Thursday, March 1, 2012 8:48:16 AM UTC-5, Walter Lee Davis wrote: >> >> >> On Feb 28, 2012, at 11:42 PM, Brandon Might wrote: >> >> > So this whole many-to-many thing has me really confused. I get how to >> set it up but I can''t seem to find anywhere that really explains the best >> way to use it in your controller. >> > >> > What I have: >> > >> > Products, Orders, Orders_Products >> > >> > The Products and Orders table has the standard stuff (Id, name, >> timestamps, etc) >> > >> > The Orders_Products table has the two id columns (order_id, product_id) >> and a quantity column. >> >> What do your models look like, exactly? What do your has_many and >> belongs_to methods read as? >> > >> > What I need to do: >> > >> > Now when I save my "cart" how do you save a new order and each product >> with its quantity? >> >> If you have set up your models correctly to explain the relationship, >> then all you need to do is get a handle to your cart: >> >> @cart = Order.find(session[:cart_id]) #for example >> >> and then add to its products collection >> >> @cart.products.create(params[:products]) #assuming you have a form for >> the products >> >> When you save @cart, the relationship will be saved to the database, when >> you find @cart a second time, the @cart.products will be available to work >> with. >> >> Walter >> > >> > This is my first app in rails so the more explanation the better. >> > >> > Thanks in advance for all the help! >> > >> > >> > -- >> > You received this message because you are subscribed to the Google >> Groups "Ruby on Rails: Talk" group. >> > To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. >> > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/VIiHWQ26cpAJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Okay. I have it 90% working now. My main problem was i had has_may :table, through: :table where it should have been has_many: :table, :through => :table. Now that I got that worked out I cant get it to save those extra columns. On Thursday, March 1, 2012 6:51:06 PM UTC-5, Brandon Might wrote:> > Correction: > > class OrdersProduct < ActiveRecord::Base > > > belongs_to :Order > belongs_to :Product > end > > On Thursday, March 1, 2012 6:03:37 PM UTC-5, Brandon Might wrote: >> >> My models: >> >> class Product < ActiveRecord::Base >> >> >> has_many :Orders_Products >> has_many :Orders, through: :Orders_Products >> end >> >> class OrdersProduct < ActiveRecord::Base >> >> >> belongs_to: Order >> belongs_to: Product >> end >> >> class Order < ActiveRecord::Base >> has_many :Order_Products >> has_many :Products, through: :Orders_Products >> >> >> end >> >> Now my next question is when I do >> @cart.products.create(session[:products]), if I have more fields in the >> session like current price (I just added that to the database) and quantity >> will it save it to the Orders_Products table or will it just ignore those >> fields? >> >> Thanks for all the help, >> -Brandon >> >> On Thursday, March 1, 2012 8:48:16 AM UTC-5, Walter Lee Davis wrote: >>> >>> >>> On Feb 28, 2012, at 11:42 PM, Brandon Might wrote: >>> >>> > So this whole many-to-many thing has me really confused. I get how to >>> set it up but I can''t seem to find anywhere that really explains the best >>> way to use it in your controller. >>> > >>> > What I have: >>> > >>> > Products, Orders, Orders_Products >>> > >>> > The Products and Orders table has the standard stuff (Id, name, >>> timestamps, etc) >>> > >>> > The Orders_Products table has the two id columns (order_id, >>> product_id) and a quantity column. >>> >>> What do your models look like, exactly? What do your has_many and >>> belongs_to methods read as? >>> > >>> > What I need to do: >>> > >>> > Now when I save my "cart" how do you save a new order and each product >>> with its quantity? >>> >>> If you have set up your models correctly to explain the relationship, >>> then all you need to do is get a handle to your cart: >>> >>> @cart = Order.find(session[:cart_id]) #for example >>> >>> and then add to its products collection >>> >>> @cart.products.create(params[:products]) #assuming you have a form for >>> the products >>> >>> When you save @cart, the relationship will be saved to the database, >>> when you find @cart a second time, the @cart.products will be available to >>> work with. >>> >>> Walter >>> > >>> > This is my first app in rails so the more explanation the better. >>> > >>> > Thanks in advance for all the help! >>> > >>> > >>> > -- >>> > You received this message because you are subscribed to the Google >>> Groups "Ruby on Rails: Talk" group. >>> > To view this discussion on the web visit >>> https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. >>> > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> . >>> > To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> > For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >>> >> On Thursday, March 1, 2012 8:48:16 AM UTC-5, Walter Lee Davis wrote: >>> >>> >>> On Feb 28, 2012, at 11:42 PM, Brandon Might wrote: >>> >>> > So this whole many-to-many thing has me really confused. I get how to >>> set it up but I can''t seem to find anywhere that really explains the best >>> way to use it in your controller. >>> > >>> > What I have: >>> > >>> > Products, Orders, Orders_Products >>> > >>> > The Products and Orders table has the standard stuff (Id, name, >>> timestamps, etc) >>> > >>> > The Orders_Products table has the two id columns (order_id, >>> product_id) and a quantity column. >>> >>> What do your models look like, exactly? What do your has_many and >>> belongs_to methods read as? >>> > >>> > What I need to do: >>> > >>> > Now when I save my "cart" how do you save a new order and each product >>> with its quantity? >>> >>> If you have set up your models correctly to explain the relationship, >>> then all you need to do is get a handle to your cart: >>> >>> @cart = Order.find(session[:cart_id]) #for example >>> >>> and then add to its products collection >>> >>> @cart.products.create(params[:products]) #assuming you have a form for >>> the products >>> >>> When you save @cart, the relationship will be saved to the database, >>> when you find @cart a second time, the @cart.products will be available to >>> work with. >>> >>> Walter >>> > >>> > This is my first app in rails so the more explanation the better. >>> > >>> > Thanks in advance for all the help! >>> > >>> > >>> > -- >>> > You received this message because you are subscribed to the Google >>> Groups "Ruby on Rails: Talk" group. >>> > To view this discussion on the web visit >>> https://groups.google.com/d/msg/rubyonrails-talk/-/NZFLIYGhDL8J. >>> > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> . >>> > To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> > For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >>>-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/pE-igg4dQNgJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 March 2012 23:03, Brandon Might <might.brandon-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My models: > > class Product < ActiveRecord::Base > > > has_many :Orders_ProductsThat should be :orders_products all lower case, so matching the table name. The same applies to all later relationships. Colin> has_many :Orders, through: :Orders_Products > end > > class OrdersProduct < ActiveRecord::Base > > > belongs_to: Order > belongs_to: Product > end > > class Order < ActiveRecord::Base > has_many :Order_Products > has_many :Products, through: :Orders_Products > > > end-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.