How to set primary key in rails 2.0.2

Dear friends,

I’m using rails 2.0.2. I have created a model sample and i have created
columns using following codes

class CreateSamples < ActiveRecord::Migration def self.up create_table :samples, :id => false do |t| t.column :user_id, :integer, :limit => 10 t.column :name, :string, :limit => 20 t.column :age, :string, :limit => 3 t.timestamps end end

def self.down
drop_table :samples
end
end

The default primary key is id and it is auto incremented. I have set id
=> false and i don’t want to use it again. I need to set my user_id as
primary key. Could any one suggest how to set the primary key other than
id in rails 2.0.2

Thanks in advance

Regards,
Jose Martin

in your user model write
set_primary_key “user_id”

On Jul 19, 9:22 am, dare ruby [email protected]

Bala kishore Pulicherla wrote:

in your user model write
set_primary_key “user_id”

On Jul 19, 9:22�am, dare ruby [email protected]

Dear bala,

Thanks for your suggestion. its working fine now.

Could you please provide details about auto incrementation for above
mentioned primary key.

Thanks in advance

Regards,
Jose Martin

u can put the id as itself as an auto increment and while ur creating
the
new user make the id to user_id and update.

for ex.
user = User.new(params[:user])
if user.save!
user_id = user.id
user.update_attribute(:user_id,"#{user.id})
respond_to |format| do
ur - stuff
end
end

mean while i’ll find any :auto_increment option is there :frowning:

On Sat, Jul 19, 2008 at 2:11 PM, dare ruby
[email protected]

t.column :user_id, :primary_key, :null => false

hope this will work for u :slight_smile:

On Sat, Jul 19, 2008 at 3:01 PM, bala kishore pulicherla <

Bala kishore Pulicherla wrote:

t.column :user_id, :primary_key, :null => false

hope this will work for u :slight_smile:

On Sat, Jul 19, 2008 at 3:01 PM, bala kishore pulicherla <

Dear Bala,

Thanks for your very valuable information. I have used the above in my
code and its working fine now

Thanks

regards,
Jose Martin

ur welcome. :slight_smile:

On Sat, Jul 19, 2008 at 4:17 PM, dare ruby
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs