Forum: Ruby on Rails Join table not being populated

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.
E4b8c35c29cfcaea3c2101193417466b?d=identicon&s=25 Gene Kahn (kublai)
on 2006-05-28 08:18
Hello,
I have the usual Categories and Products table scenario, mediated by a
Categories_Products join table. When I create a new Product and into a
given Category, I notice that my join table is not being populated.
Shouldn't the join Categories_Products table be populated?
Thanks,
gk
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-05-28 08:40
Gene Kahn wrote:
> I have the usual Categories and Products table scenario, mediated by a
> Categories_Products join table. When I create a new Product and into a
> given Category, I notice that my join table is not being populated.
> Shouldn't the join Categories_Products table be populated?

I assume you're doing
  category.products.create(...)
There's a bug in create so it doesn't populate the join table. I'm
working on the bug, hope to have a fix soon. In the mean time, just use
<< like so:
  category.products << Product.create(...)

--
Josh Susser
http://blog.hasmanythrough.com
E4b8c35c29cfcaea3c2101193417466b?d=identicon&s=25 Gene Kahn (kublai)
on 2006-05-28 09:45
Thanks for your reply. Since the join table has no model, and it has no
controller of its own, where would I put such a statement? Any
site/reference where I can learn about it?
Thanks,
gk

Josh Susser wrote:
> Gene Kahn wrote:
>> I have the usual Categories and Products table scenario, mediated by a
>> Categories_Products join table. When I create a new Product and into a
>> given Category, I notice that my join table is not being populated.
>> Shouldn't the join Categories_Products table be populated?
>
> I assume you're doing
>   category.products.create(...)
> There's a bug in create so it doesn't populate the join table. I'm
> working on the bug, hope to have a fix soon. In the mean time, just use
> << like so:
>   category.products << Product.create(...)
>
> --
> Josh Susser
> http://blog.hasmanythrough.com
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-05-28 10:02
(Received via mailing list)
On 5/28/06, Gene Kahn <kublaikhan55@hotmail.com> wrote:
> Thanks for your reply. Since the join table has no model, and it has no
> controller of its own, where would I put such a statement? Any

Try using the "category.products << Product.create(...)" where you'd
normally put Product.create(...).

> site/reference where I can learn about it?
> Thanks,
> gk

Your question is related to ActiveRecord associations, so:

http://api.rubyonrails.com/classes/ActiveRecord/As...

Of course, the basic AR docs are also relevant:

http://api.rubyonrails.com/classes/ActionWebServic...
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-05-28 10:12
(Received via mailing list)
Error correction: the second link (to AR::Base) is actually

http://api.rubyonrails.com/classes/ActiveRecord/Base.html
E4b8c35c29cfcaea3c2101193417466b?d=identicon&s=25 Gene Kahn (kublai)
on 2006-05-28 19:36
Alder Green wrote:
> Error correction: the second link (to AR::Base) is actually
>
> http://api.rubyonrails.com/classes/ActiveRecord/Base.html

Thanks!
gk
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-05-29 06:50
I got the fix for this issue accepted to the build today. You can read
about it here:

http://blog.hasmanythrough.com/articles/2006/05/28...

Note that there's a plugin so you can get the fix if you're not running
on trunk.

--
Josh Susser
http://blog.hasmanythrough.com
E4b8c35c29cfcaea3c2101193417466b?d=identicon&s=25 Gene Kahn (kublai)
on 2006-05-29 07:29
Josh Susser wrote:
> I got the fix for this issue accepted to the build today. You can read
> about it here:
>
> http://blog.hasmanythrough.com/articles/2006/05/28...
>
> Note that there's a plugin so you can get the fix if you're not running
> on trunk.
>
> --
> Josh Susser
> http://blog.hasmanythrough.com

Thanks much again!
gk
This topic is locked and can not be replied to.