Rails - habtm 关联未保存到数据库
产品控制器:
def update
params[:product][:category_ids] ||= []
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
redirect_to @product
else
render "edit"
end
表单:
<% for category in Category.all %>
<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
<%= category.name %>
<% end %>
当我查看日志时,看起来它更新了类别,但是当我转到rails控制台并查找产品时,它告诉我category_id为零,所以当我搜索时某个产品类别中的所有产品,它不会返回任何内容,因为所有的category_id都是nil。
我有一个产品和一个类别模型以及一个简单的categories_products 连接表。我尝试在产品模型中使用accepts_nested_attributes_for,并进行了相应的表单更改,但这也不起作用。
有谁知道为什么它不保存到数据库级别?同样在这种情况下,是按照我这里的方式进行操作还是使用嵌套属性更好?谢谢。
UPDATE
来自日志的代码
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "product"=>{"remote_image_url"=>"", "name"=>"Test", "manufacturer"=>"Unknown", "category_ids"=>["1"], "price"=>"$25.00", "available"=>"true", "commit"=>"Update", "id"=>"1"}
Category Load (0.5ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
Category Load (0.2ms) SELECT * FROM `categories` INNER JOIN `categories_products` ON `categories`.id = `categories_products`.category_id WHERE (`categories_products`.product_id = 1 )
AREL (0.4ms) DELETE FROM `categories_products` WHERE `categories_products`.`product_id` = 1 AND `categories_products`.`category_id` IN (2)
AREL (0.4ms) UPDATE `products` SET `price` = 0.0, `updated_at` = '2011-10-03 16:48:01' WHERE `products`.`id` = 1
SQL (1.8ms) COMMIT
来自产品模型的
attr_accessible :name, :manufacturer, :price, :category_ids, :image, :remote_image_url, :available
has_and_belongs_to_many :categories
代码 来自类别模型的代码
has_and_belongs_to_many :products
Product controller:
def update
params[:product][:category_ids] ||= []
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
redirect_to @product
else
render "edit"
end
form:
<% for category in Category.all %>
<%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %>
<%= category.name %>
<% end %>
When I look at the log, it looks like it updated the category, but when I go to rails console and did a lookup for the product, it tells me that the category_id is nil so when I do a search for all products in a certain product category, it doesn't return anything because all the category_id is nil.
I have a product and a category model and a simple join table for categories_products. I tried using accepts_nested_attributes_for in the product model and made the respective form changes as well and that didn't work either.
Does anyone know why it doesn't save to the database level? Also in a situation like this, is it better to do it the way I have it here or to do nested attributes? Thanks.
UPDATE
Code from log
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "product"=>{"remote_image_url"=>"", "name"=>"Test", "manufacturer"=>"Unknown", "category_ids"=>["1"], "price"=>"$25.00", "available"=>"true", "commit"=>"Update", "id"=>"1"}
Category Load (0.5ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
Category Load (0.2ms) SELECT * FROM `categories` INNER JOIN `categories_products` ON `categories`.id = `categories_products`.category_id WHERE (`categories_products`.product_id = 1 )
AREL (0.4ms) DELETE FROM `categories_products` WHERE `categories_products`.`product_id` = 1 AND `categories_products`.`category_id` IN (2)
AREL (0.4ms) UPDATE `products` SET `price` = 0.0, `updated_at` = '2011-10-03 16:48:01' WHERE `products`.`id` = 1
SQL (1.8ms) COMMIT
Code from product model
attr_accessible :name, :manufacturer, :price, :category_ids, :image, :remote_image_url, :available
has_and_belongs_to_many :categories
Code from category model
has_and_belongs_to_many :products
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你无法用“habtm”来完成你想要做的事情。查看此问题的答案,了解如何将 habtm 更改为 < code>has_many :through 关联,并使用表单中的联接表模型来更新关联。
I think you can't accomplish what you are trying to do with a "habtm". Take a look at the answer to this question about changing the habtm to a
has_many :through
association, and using the join table model in your form to update the association.