Ruby on Rails
Monday, November 25, 2013
Hi
I have a problem with has_many :trough association. I have 2 tables, Product and Category, and when I create new Product, I should make checkboxes to choose in what categories the product . I've made model category_product where I store the association from the Category and Product tables. Here is some of the code.
class Category < ActiveRecord::Base
has_many :category_products, dependent: :destroy
has_many :products, :through => :category_products
accepts_nested_attributes_for :category_products
class CategoryProduct < ActiveRecord::Base
belongs_to :category
belongs_to :product
accepts_nested_attributes_for :category
class Product < ActiveRecord::Base
has_many :category_products, dependent: :destroy
has_many :categories, :through => :category_products
accepts_nested_attributes_for :category_products
class ProductsController < ApplicationController
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
items_array = category_params
items_array.each do |key, value|
if value != ""
@category_product = CategoryProduct.new(:category => value, :product => @product.id)
@category_product.save
end
#category_products.create!(:category => arrayitem.id, :product => @product.id)
end
#@categories = Category.find(category_params)
# @categories.each do |category|
#category_products.create!(:category => category.id, :product => @product.id)
I have a problem with has_many :trough association. I have 2 tables, Product and Category, and when I create new Product, I should make checkboxes to choose in what categories the product . I've made model category_product where I store the association from the Category and Product tables. Here is some of the code.
class Category < ActiveRecord::Base
has_many :category_products, dependent: :destroy
has_many :products, :through => :category_products
accepts_nested_attributes_for :category_products
class CategoryProduct < ActiveRecord::Base
belongs_to :category
belongs_to :product
accepts_nested_attributes_for :category
class Product < ActiveRecord::Base
has_many :category_products, dependent: :destroy
has_many :categories, :through => :category_products
accepts_nested_attributes_for :category_products
class ProductsController < ApplicationController
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if @product.save
items_array = category_params
items_array.each do |key, value|
if value != ""
@category_product = CategoryProduct.new(:category => value, :product => @product.id)
@category_product.save
end
#category_products.create!(:category => arrayitem.id, :product => @product.id)
end
#@categories = Category.find(category_params)
# @categories.each do |category|
#category_products.create!(:category => category.id, :product => @product.id)
Subscribe to:
Post Comments (Atom)
Blog Archive
- October 2025 (2)
- August 2025 (2)
- August 2024 (1)
- April 2024 (1)
- July 2023 (1)
- February 2023 (38)
- November 2021 (1)
- January 2021 (1)
- December 2020 (1)
- March 2020 (49)
- February 2020 (59)
- January 2020 (109)
- December 2019 (130)
- November 2019 (77)
- October 2019 (106)
- September 2019 (66)
- August 2019 (96)
- July 2019 (27)
- June 2019 (52)
- May 2019 (51)
- April 2019 (53)
- March 2019 (115)
- February 2019 (153)
- January 2019 (222)
- December 2018 (155)
- November 2018 (104)
- October 2018 (164)
- September 2018 (89)
- August 2018 (209)
- July 2018 (126)
- June 2018 (171)
- May 2018 (80)
- April 2018 (81)
- March 2018 (126)
- February 2018 (161)
- January 2018 (153)
- December 2017 (55)
- November 2017 (53)
- October 2017 (96)
- September 2017 (250)
- August 2017 (99)
- July 2017 (253)
- June 2017 (175)
- May 2017 (94)
- April 2017 (140)
- March 2017 (126)
- February 2017 (148)
- January 2017 (80)
- December 2016 (116)
- November 2016 (56)
- October 2016 (105)
- September 2016 (38)
- August 2016 (100)
- July 2016 (173)
- June 2016 (192)
- May 2016 (177)
- April 2016 (211)
- March 2016 (318)
- February 2016 (269)
- January 2016 (346)
- December 2015 (363)
- November 2015 (380)
- October 2015 (269)
- September 2015 (365)
- August 2015 (287)
- July 2015 (539)
- June 2015 (430)
- May 2015 (337)
- April 2015 (453)
- March 2015 (350)
- February 2015 (386)
- January 2015 (436)
- December 2014 (431)
- November 2014 (371)
- October 2014 (319)
- September 2014 (423)
- August 2014 (475)
- July 2014 (505)
- June 2014 (444)
- May 2014 (601)
- April 2014 (489)
- March 2014 (486)
- February 2014 (396)
- January 2014 (429)
- December 2013 (323)
- November 2013 (371)
- October 2013 (488)
- September 2013 (438)
- August 2013 (442)
- July 2013 (651)
- June 2013 (557)
- May 2013 (751)
- April 2013 (582)
- March 2013 (775)
- February 2013 (588)
- January 2013 (697)
- December 2012 (682)
- November 2012 (830)
- October 2012 (879)
- September 2012 (1091)
- August 2012 (851)
- July 2012 (872)
- June 2012 (874)
- May 2012 (845)
- April 2012 (979)
- March 2012 (1003)
- February 2012 (1272)
- January 2012 (1291)
- December 2011 (1184)
- November 2011 (1160)
- October 2011 (1107)
- September 2011 (1184)
- August 2011 (1451)
- July 2011 (1313)
- June 2011 (1174)
- May 2011 (1351)
- April 2011 (1364)
- March 2011 (1429)
- February 2011 (1332)
- January 2011 (1427)
- December 2010 (1464)
- November 2010 (1456)
- October 2010 (1460)
- September 2010 (1467)
- August 2010 (1496)
- July 2010 (1500)
- June 2010 (1411)
- May 2010 (1418)
- April 2010 (1054)
No comments:
Post a Comment