Forum: Ruby on Rails :table_name_with_underscore and join models

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.
Greg D. (Guest)
on 2008-11-04 01:17
(Received via mailing list)
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?
Greg D. (Guest)
on 2008-11-04 01:21
(Received via mailing list)
On Mon, Nov 3, 2008 at 5:02 PM, gdonald <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/
Greg D. (Guest)
on 2008-11-04 18:55
(Received via mailing list)
On Mon, Nov 3, 2008 at 5:02 PM, gdonald <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/
Greg D. (Guest)
on 2008-11-04 18:57
(Received via mailing list)
On Tue, Nov 4, 2008 at 10:54 AM, Greg D. <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/
This topic is locked and can not be replied to.