Sweety Vamshi
2007-Nov-30 17:23 UTC
cross sell products - Options_from_collection_select ?
Hi I have collected some code for cross sell products on this website. CREATE TABLE products ( id INT NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, PRIMARY KEY (id) ); INSERT INTO products VALUES (1, ''alpha''); INSERT INTO products VALUES (2, ''beta''); INSERT INTO products VALUES (3, ''gamma''); CREATE TABLE cross_sells ( product_id INT NOT NULL, cross_sell_product_id INT NOT NULL, cs_position INT NOT NULL, PRIMARY KEY (product_id, cross_sell_product_id), UNIQUE KEY pos (cs_position) ); #cross sell beta(2) and gamma(3) on alpha(1) INSERT INTO cross_sells VALUES (1,2,1); INSERT INTO cross_sells VALUES (1,3,2); My product model in product.rb was this class Product < ActiveRecord::Base has_and_belongs_to_many :cross_sell_products, :join_table => ''cross_sells'', :foreign_key => ''product_id'', :association_foreign_key => ''cross_sell_product_id'', :class_name => ''Product'', :order => ''cs_position'' has_and_belongs_to_many :cross_seller_products, :join_table => ''cross_sells'', :foreign_key => ''cross_sell_product_id'', :association_foreign_key => ''product_id'', :class_name => ''Product'' end My view template in product.rhtml could easily list links to the cross sell products <h2>Product: <%= @product.name %></h2> <h4>Cross Sell Products</h4> <p>May we also suggest:</p> <% @product.cross_sell_products.each do |cross_sell_product|%> <%= link_to cross_sell_product.name, :action => ''product'', :id => cross_sell_product.id %><br /> <% end %> ------------> My Question is how do we create an options_for_collection_select ?in the -form.html <!-- cross sells --> <label for "product_crosssells"> Cross Sell</label> <select name=''products[cross_sell_products][]'' multiple=''multiple''> <%= @products = Product.find(:all, :order => "name") options_for_select @products.collect {|u| [u.name, u.id]}, @product.cross_sell_products %> </select> Its not updating to the database? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---