Thanks. I am sure it is something I am missing. I am trying to learn
about SQL and Rails.
Here is the script I am using. I am really just trying to create a
script that explores all of the database relationships and how to
implement them in Rails.

The code: http://rafb.net/p/iUgqjX46.html

The output and SQL: http://rafb.net/p/tpvlVc44.html


On Mar 17, 2:48 am, "\"Wolas!\"" <[email protected]> wrote:
> I replicated 2 scenareos and neither of them do what you have
> described:
>
> has and belongs to many scenario
>
> == models
>
> class Product
>   has_and_belongs_to_many :categories
> end
>
> class Category
>   has_and_belongs_to_many :products
> end
>
> == db
>
> create_table "categories", :force => true do |t|
>     t.string   "name"
>   end
>
>   create_table "categories_products", :id => false, :force => true do |
> t|
>     t.integer "product_id"
>     t.integer "category_id"
>   end
>
>   create_table "products", :force => true do |t|
>     t.string   "name"
>     t.integer  "price"
>   end
>
> == test
>
> >> p = Product.new :name => "cool bike", :price => 123
>
> => #<Product id: nil, name: "cool bike", price: 123>>> p.categories << c
>
> => [#<Category id: 1, name: "bikes">]>> p.save
> => true
> >> p.categories
>
> => [#<Category id: 1, name: "bikes">]
>
> Same thing with has_many :through. which leads me to believe you
> havent set your relationships right. Could you show us the mdoel and
> table code plz?
>
> On Mar 17, 5:11 am, ball <[email protected]> wrote:
>
> > It didn't exist before hand. ActiveRecord is triggering two inserts. I
> > have included a full example and the SQL
>
> >   catBike = Category.new(:name => "Bikes")
> >   catBike.save
>
> >   p = Product.new(:name => "Cannondale Bike", :price => 1000)
> >   p.save
> >   p.categories << catBike # This works great.
>
> >   p = Product.new(:name => "Trek Bike", :price => 900)
> >   p.categories << catBike # this causes two entriens to be created
> >   p.save
>
> >  Category Create (1.0ms)   INSERT INTO `categories` (`name`) VALUES
> > ('Bikes')
> >  Product Create (0.0ms)   INSERT INTO `products` (`name`, `price`)
> > VALUES('Cannondale Bike', 1000)
> >  SQL (0.0ms)   COMMIT
> >  SQL (0.0ms)   BEGIN
> >  categories_products Columns (10.0ms)   SHOW FIELDS FROM
> > `categories_products`
> >  SQL (1.0ms)   INSERT INTO `categories_products` (`product_id`,
> > `category_id`) VALUES (1, 1)
> >  SQL (0.0ms)   COMMIT
> >  SQL (0.0ms)   BEGIN
> >  SQL (0.0ms)   COMMIT
> >  SQL (0.0ms)   BEGIN
> >  Product Create (1.0ms)   INSERT INTO `products` (`name`, `price`)
> > VALUES('Trek Bike', 900)
> >  categories_products Columns (7.0ms)   SHOW FIELDS FROM
> > `categories_products`
> >  SQL (1.0ms)   INSERT INTO `categories_products` (`product_id`,
> > `category_id`) VALUES (2, 1)
> >  categories_products Columns (7.0ms)   SHOW FIELDS FROM
> > `categories_products`
> >  SQL (0.0ms)   INSERT INTO `categories_products` (`product_id`,
> > `category_id`) VALUES (2, 1)
> >  SQL (0.0ms)   COMMIT
>
> > On Mar 16, 4:50 pm, Phlip <[email protected]> wrote:
>
> > > > p.categories now has TWO catBike
>
> > > Because << does not ask questions. It just adds a catBike. If you already 
> > > had
> > > one, now you have two.
>
> > > You might need some combination of these:
>
> > >    a :unique => true on the habtm (spelling?)
> > >    a unique key (index) on the database
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to