Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding extra attribute to a rails command

While trying to generate a new scaffold using - collection> rails generate scaffold Items name:string subcategory_id:integer users_id:integer, i missed out the 'users_id:integer' and way i can go about having this attribute on it and not by running the full command again.

Thanks

like image 830
El nino Avatar asked Dec 05 '11 11:12

El nino


2 Answers

If you haven't already pushed the migration, you could edit db/migrate/201112*create_items.rb and add the following line:

add_column :items, :users_id, :integer

If you have already pushed the migration, create another:

rails generate migration AddUsersIdToItems users_id:integer
rake db:migrate

Then you'll have to manually edit the scaffold code:

vi app/views/items/_form.html.erb

Copy the <div> for subcategory_id and change it to :users_id

<div class="field">
  <%= f.label :subcategory_id %><br />
  <%= f.number_field :subcategory_id %>
</div>
<div class="field">
  <%= f.label :users_id %><br />
  <%= f.number_field :users_id %>
</div>

Alternatively, if you're running git, you could have run "git checkout ." after running the first scaffold generator, which would undo all your changes, then you could run it again.

like image 78
Jason Noble Avatar answered Sep 24 '22 01:09

Jason Noble


Not really.

If you haven't added anything new to the existing scaffold yet (and are just worried about having to delete/recreate all those files) then you can run destroy first then recreate eg:

rails destroy scaffold Items
rails generate scaffold Items name:string subcategory_id:integer users_id:integer
like image 34
Taryn East Avatar answered Sep 23 '22 01:09

Taryn East