Forum: Ruby on Rails why does this happen...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B15131e5df68c72103eac27911680873?d=identicon&s=25 ball (Guest)
on 2009-03-16 23:41
(Received via mailing list)
Quick question, using ActiveRecord,

  catBike = Category.new(:name => "Bikes")
  catBike.save

  p = Product.new(:name => "Bike", :price => 900)
  p.categories << catBike
  p.save

p.categories now has TWO catBike

however, if I move the save before adding the categories it is fine.

I am curious why this happens. Thanks!
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-17 00:51
(Received via mailing list)
> 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
B15131e5df68c72103eac27911680873?d=identicon&s=25 ball (Guest)
on 2009-03-17 05:12
(Received via mailing list)
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
872535e214a9e3a98889bd8d069f0294?d=identicon&s=25 "Wolas!" (Guest)
on 2009-03-17 10:48
(Received via mailing list)
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?
B15131e5df68c72103eac27911680873?d=identicon&s=25 ball (Guest)
on 2009-03-17 15:18
(Received via mailing list)
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
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 rubyguy@ymail.com (Guest)
on 2009-03-17 16:13
(Received via mailing list)
On 17 Mar., 15:18, ball <chezb...@gmail.com> wrote:
> 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

I believe it's because the << method both append the specified object
to the collection (in your case, a Category to a Products categories
collection) AND saves it afterwards. So saving the product twice might
cause the category to be added twice. At least that's what I think. So
in your case I'ld do like this:

cat_bike = Category.new(:name => "Bikes")
cat_bike.save

p = Product.new(:name => "Bike", :price => 900)
p.save
p.categories << cat_bike

To avoid duplication in your categories table. Hope that was
helpful. :)

--
Cheers,
David Knorr
http://twitter.com/rubyguy
B15131e5df68c72103eac27911680873?d=identicon&s=25 ball (Guest)
on 2009-03-17 23:11
(Received via mailing list)
Thanks David, I poked around in the debugger a bit after your
suggestion, and honestly, I don't understand Ruby code well enough to
make sense of it all. Not to mention, I kept running into
>> No sourcefile available for (eval)

However, what I did find was that the << operator, did not issue the
insert command in the "double" case. The save command after the fact
seemed to issue the two inserts. Now this confuses me completely.

So, if anyone can explain why/how this could happen, that would be
great.
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-03-18 19:24
(Received via mailing list)
RAFB apparently drops files after 24 hours - any chance you could put
those examples up someplace (gist or pastie, for instance) with a
little more persistence?

Without seeing the code, it's hard to even guess what's going on. But
I seem to recall seeing similar behavior when I had code saving an
associated record in before_save, which then got picked up again by
the association save callbacks.

--Matt Jones
36ed63ef68c172f148c491418395d0b5?d=identicon&s=25 rubyguy@ymail.com (Guest)
on 2009-03-20 21:35
(Received via mailing list)
On 18 Mar., 19:23, Matt Jones <al2o...@gmail.com> wrote:
> RAFB apparently drops files after 24 hours - any chance you could put
> those examples up someplace (gist or pastie, for instance) with a
> little more persistence?
>
> Without seeing the code, it's hard to even guess what's going on. But
> I seem to recall seeing similar behavior when I had code saving an
> associated record in before_save, which then got picked up again by
> the association save callbacks.

I totally agree. Ball, you should really paste some code somewhere, so
we can try to figure out what's wrong here.

--
Cheers,
David Knorr
http://twitter.com/rubyguy
This topic is locked and can not be replied to.