Hola este mi primer mail de la lista
Bueno les comento que me estoy cambiando a Ruby on Rails y dejando de
programar en el estimado PhP
Estoy desarrollando una aplicacion que vi en RailsCasts se trata de los
siguiente:
Tabla Products
CREATE TABLE products
(
id
tinyint(100) NOT NULL auto_increment,
name
varchar(100) NOT NULL,
price
decimal(10,2) NOT NULL,
PRIMARY KEY (id
)
)
Tabla Categories
CREATE TABLE categories
(
id
tinyint(100) NOT NULL auto_increment,
name
varchar(100) NOT NULL,
PRIMARY KEY (id
)
)
Tabla
categories_productshttp://localhost/phpmyadmin/tbl_properties_structure.php?db=checkbox&table=categories_products&token=6a8ff948433a4e35e863c17801023afb
CREATE TABLE categories_products
(
id
tinyint(100) NOT NULL auto_increment,
category_id
tinyint(100) NOT NULL,
product_id
tinyint(100) NOT NULL,
PRIMARY KEY (id
)
)
Los productos van tener varias categorias y las categorias van a tener
varias productos entonces hago los modelos de la siguiente manera
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
end
Despues creo el edit.rhtml
<%= start_form_tag :action => ‘update’ , :id => @product %>
<%= render :partial => ‘form’ %>
<%= submit_tag ‘Edit’%>
<%= end_form_tag %>
<%= link_to ‘Show’, :action => ‘show’, :id => @product %> |
<%= link_to ‘Back’, :action => ‘list’ %>
y tambien un partial _form.rhtml
<%= error_messages_for ‘products’ %>
Name
<%= text_field :product, :name %>
Price
<%= text_field :product, :price %>
<% for category in Category.find(:all) %>
</div>
<% end %>
Y ahora en mi controlador de Products tengo el siguiente codigo
class ProductController < ApplicationController
scaffold :Product
def edit
@product=Product.find(@params["id"])
end
def update
@product = Product.find(@params['id'])
if @product.update_attributes(@params['product'])
flash['notice'] = "Update Successful"
redirect_to :action => 'list'
else
flash['notice'] = "Error en Actualizar"
redirect_to :action => 'list'
end
end
end
La aplicacion funciona en algunos campos pero en otros me da este error
Mysql::Error: Duplicate entry ‘2’ for key 1: INSERT INTO
categories_products (product_id
, id
, category_id
) VALUES (5, 2,
2)
Lo que entiendo que la tabla se duplica los registros
Bueno agradezco de la ayuda , yo seguiré buscando mas info
Gracias