# :table\_name\_with\_underscore and join models

**URL:** <https://www.ruby-forum.com/t/table-name-with-underscore-and-join-models/152443>\
**Category:** Rails\
**Created:** [November 4, 2008, 12:17am UTC](https://www.ruby-forum.com/t/table-name-with-underscore-and-join-models/152443 "2008-11-04T00:17:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [November 4, 2008, 12:17am UTC](https://www.ruby-forum.com/t/table-name-with-underscore-and-join-models/152443/1 "2008-11-04T00:17:16Z")

</div>

Per my PHBs, I’m using

ActiveRecord::Base.primary\_key\_prefix\_type  
= :table\_name\_with\_underscore

in my current project.

My problem is that this setting seems to be causing me some issues  
with my join model’s primary keys.

For example I have these three models:

class User \< ActiveRecord::Base  
has\_many :user\_ugroups  
has\_many :ugroups, :through =\> :user\_ugroups  
end

class Ugroup \< ActiveRecord::Base  
has\_many :user\_ugroups  
has\_many :users, :through =\> :user\_ugroups  
end

class UserUgroup \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :ugroup  
end

My migration for the join table looks like this:

def self.up  
create\_table :user\_ugroups, :primary\_key =\> :user\_ugroups\_id do |t|  
t.integer :user\_id, :null =\> false  
t.integer :ugroup\_id, :null =\> false  
end  
end

So when I create a UserUgroup like so:

> > UserUgroup.create!( :user =\> @gdonald, :ugroup =\> @admin )

ActiveRecord tries to insert a NULL for the user\_ugroups\_id field and  
then I get this:

ActiveRecord::StatementInvalid: OCIError: ORA-01400: cannot insert  
NULL into (“MYPROJDB”.“USER\_UGROUPS”.“USER\_UGROUPS\_ID”): INSERT INTO  
user\_ugroups (user\_ugroups\_id, user\_id, ugroup\_id) VALUES(NULL, 1, 1)

The migration creates my USER\_UGROUPS\_SEQ without issue, but then it  
doesn’t get used.

Any ideas?

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [November 4, 2008, 12:21am UTC](https://www.ruby-forum.com/t/table-name-with-underscore-and-join-models/152443/2 "2008-11-04T00:21:22Z")

</div>

On Mon, Nov 3, 2008 at 5:02 PM, gdonald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ActiveRecord::Base.primary\_key\_prefix\_type  
> = :table\_name\_with\_underscore
> 
> in my current project.
> 
> My problem is that this setting seems to be causing me some issues  
> with my join model’s primary keys.

I forgot to mention, I also tried

set\_sequence\_name “USER\_UGROUPS\_SEQ”

in my join model, but I got the same error.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [November 4, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/table-name-with-underscore-and-join-models/152443/3 "2008-11-04T17:57:22Z")

</div>

On Tue, Nov 4, 2008 at 10:54 AM, Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > The migration creates my USER\_UGROUPS\_SEQ without issue, but then it  
> > doesn’t get used.
> 
> For the next poor Oracle user who comes along, here’s a workaround:
> 
> class UserUgroup \< ActiveRecord::Base
> 
> before\_save :get\_id

Oops… that should be before\_create

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![destiney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@destiney](https://www.ruby-forum.com/u/destiney)\
**Post date:** [November 4, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/table-name-with-underscore-and-join-models/152443/4 "2008-11-04T17:55:01Z")

</div>

On Mon, Nov 3, 2008 at 5:02 PM, gdonald [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> doesn’t get used.  
> For the next poor Oracle user who comes along, here’s a workaround:

class UserUgroup \< ActiveRecord::Base

before\_save :get\_id

belongs\_to :ugroup  
belongs\_to :user

def get\_id  
self.user\_ugroup\_id = connection.next\_sequence\_value(  
self.class.sequence\_name )  
end

end

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
