0

I've got a problem with an id column created with a scaffold. I generated a scaffold with following command:

rails generate scaffold scaffoldname name:string id:integer

I used that id column for a relation which I wanted to use for a dropdown menu with collection_select. Afterwards I realized that generating an id is unnecessary because of the id which rails creates automatically for each table.

When I wanted to call the related table with the self-created id this is of course possible with

class.relatedClass.id

Afterwards I realized that this command is also possible in a table where rails created the id, although the column is named "rowid" in the table.

So i thought that It should be possible to delete the self-created id column with a migration. The way I thought about this was, that rails should then use automatically the rowid for the relation. But after deleting the self-created id there are errors all around. Rails refuses to use the 'rowid' column automatically although it does it in case there was no id column specified in the scaffold command.

How do I delete that self-created id column in a way that rails uses their own created rowid afterwards when calling class.relatedClass.id?

coderuby
  • 1,188
  • 1
  • 11
  • 26

2 Answers2

2

Run this in rails terminal -

rails g migration remove_id_from_scaffoldname id:integer

 rails g migration add_rowid_to_scaffoldname rowid:integer

This will create two migration files - one to remove id field and the other to add the rowid field.

Then run this in rails terminal -

   rake db:migrate

Also note that rails will create id field automatically but not the rowid as your primary key. To restrict rails to use your defined primary key you have to define so explicitly like this here -

Your Migration file -

create_table :tablename, :primary_key => :rowid do |t|
 # ...
end

Your Model -

class ModelName< ActiveRecord::Base
  self.primary_key = "rowid"
  ...
end

Note: It is a good practice to use rails auto generated id as a primary key unless otherwise you have a valid reason to do so.

sjain
  • 23,126
  • 28
  • 107
  • 185
  • ok, I think that I got it. My failure was that I thought the id column, which rails generates, is the 'rowid' which i can see in all my tables when i look at it with SQliteManager. But it seems that this column is only something like the row labels in excel (1..n) and not created from rails itself. I see now the sql query in SQliteManager which creates the output: `SELECT rowid, * FROM "testmodels";´. thanks for the hint that rowid isn't the rails generated id! – coderuby Jan 17 '13 at 17:50
1

One thing you can do is run the down migration, remove the id field from the migration and run the up migration again.

When you ran the generate, it created a migration file something like:

db/migrate/20130117134712_create_scaffoldnames.rb

if you do an ls of db/migrate, you will see other migrations

> ls db/migrate
20130114170853_create_server_requests.rb
20130117134712_create_scaffoldnames.rb

You can run the down migration to 20130117134712_create_scaffoldnames by running db:migrate to the previous version

> rake db:migrate VERSION=20130114170853

Then edit your scaffoldnames migration the way you want it, and run db:migrate again.

I do this all the time. The trick is to catch these modifications while they are local to my development environment, so I can do these thing.

Also, if you catch it early enough, you can run

rails destroy scaffold scaffoldname

to undo the scaffold generation.

Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52
  • Why are you including the `id:integer` in the `rails generate` command? Couldn't that lead to some kind of unwanted duplication? – Thomas Klemm Jan 17 '13 at 14:03
  • Maybe that is misleading, as it is not part of my answer. I am commenting that the create in the question is a typographical error. I'll edit my posts to be more clear. – Marlin Pierce Jan 17 '13 at 15:08
  • Indeed, the 'create' is a typographical error. Sorry for that! Will correct it! – coderuby Jan 17 '13 at 17:56