Next step: I have succeeded in adding a column to a database table,
then
using the generator tools to update the Rails objects that implement
that
model. The process, which I share for what it’s worth to others, is:
-
Add attribute *toys.color* (text):
sqlite> ALTER TABLE toys ADD COLUMN color TEXT;
sqlite> INSERT INTO toys (name, color) VALUES (‘Tinker’, ‘Green’);
sqlite> select * from toys;
1|Tinker|Green
-
Ran rake db:schema:dump to update schema.rb:
ActiveRecord::Schema.define(version: 0) do
create_table “toys”, force: :cascade do |t|
t.text "name"
*t.text "color"*
end
-
Ran *schema_to_scaffold* for *toys*:
[sunward@web324 x]$ scaffold -p db
…
Loaded tables:
-
toys
-
users
…
Select a table: 0
Script for scaffold:
rails generate scaffold Toy name:text color:text --no-migration
-
Ran *scaffold *using that script:
[sunward@web324 x]$ rails generate scaffold Toy name:text color:text
–no-migration
invoke active_record
conflict app/models/toy.rb
Overwrite /home/sunward/webapps/rails_eval/x/app/models/toy.rb? (enter
“h” for help) [Ynaqdh] Y
force app/models/toy.rb
invoke test_unit
create test/models/toy_test.rb
create test/fixtures/toys.yml
invoke resource_route
route resources :toys
invoke scaffold_controller
conflict app/controllers/toys_controller.rb
Overwrite
/home/sunward/webapps/rails_eval/x/app/controllers/toys_controller.rb?
(enter “h” for help) [Ynaqdh] Y
force app/controllers/toys_controller.rb
invoke erb
exist app/views/toys
conflict app/views/toys/index.html.erb
Overwrite
/home/sunward/webapps/rails_eval/x/app/views/toys/index.html.erb? (enter
“h” for help) [Ynaqdh] Y
force app/views/toys/index.html.erb
conflict app/views/toys/edit.html.erb
Overwrite
/home/sunward/webapps/rails_eval/x/app/views/toys/edit.html.erb? (enter
“h”
for help) [Ynaqdh] Y
force app/views/toys/edit.html.erb
conflict app/views/toys/show.html.erb
Overwrite
/home/sunward/webapps/rails_eval/x/app/views/toys/show.html.erb? (enter
“h”
for help) [Ynaqdh] Y
force app/views/toys/show.html.erb
conflict app/views/toys/new.html.erb
Overwrite
/home/sunward/webapps/rails_eval/x/app/views/toys/new.html.erb? (enter
“h”
for help) [Ynaqdh] Y
force app/views/toys/new.html.erb
conflict app/views/toys/_form.html.erb
Overwrite
/home/sunward/webapps/rails_eval/x/app/views/toys/_form.html.erb? (enter
“h” for help) [Ynaqdh] Y
force app/views/toys/_form.html.erb
invoke test_unit
conflict test/controllers/toys_controller_test.rb
Overwrite
/home/sunward/webapps/rails_eval/x/test/controllers/toys_controller_test.rb?
(enter “h” for help) [Ynaqdh] Y
force test/controllers/toys_controller_test.rb
invoke helper
identical app/helpers/toys_helper.rb
invoke test_unit
invoke jbuilder
conflict app/views/toys/index.json.jbuilder
Overwrite
/home/sunward/webapps/rails_eval/x/app/views/toys/index.json.jbuilder?
(enter “h” for help) [Ynaqdh] Y
force app/views/toys/index.json.jbuilder
conflict app/views/toys/show.json.jbuilder
Overwrite
/home/sunward/webapps/rails_eval/x/app/views/toys/show.json.jbuilder?
(enter “h” for help) [Ynaqdh] Y
force app/views/toys/show.json.jbuilder
invoke assets
invoke coffee
identical app/assets/javascripts/toys.coffee
invoke scss
conflict app/assets/stylesheets/toys.scss
Overwrite
/home/sunward/webapps/rails_eval/x/app/assets/stylesheets/toys.scss?
(enter
“h” for help) [Ynaqdh] Y
force app/assets/stylesheets/toys.scss
invoke scss
identical
-
*It **work**s!* See http://x.sunward.webfactional.com/toys/.
However: This process has only a limited ability to update the Rails
objects to handle the database change while preserving manually entered
changes to them. Specifically: For each Rails file where the new
(generated) version differs from the existing (possibly manually
modified)
version, you get to choose between keeping the old version (so it
doesn’t
match the database changes) or replacing it with the new version (so any
manual updates to the old version are lost). In other words, I haven’t
found a way to generate a new file that is a combination of the old and
the
new - that is, it is identical to the old file in all respects except
that
it implements the database schema changes.
So…
Next question: Are there any Rails tools that can generate a
“combined” file in the sense just defined? Is there any way to generate
a
“combined” file in the sense just defined? - either with other Rails
tools
or with options to the tools used in the above scenarios?
I know that meaningfully merging two versions of an object quickly gets
us
into the “hard problem” zone. But I notice that the prompts that
*scaffold
*puts out do offer to show you a “diff” of the old vs new versions of
each
file. It shouldn’t be too hard to give the user the opportunity to
choose
between the old and the new versions of each line that differs, and that
would enable the user to produce the desired “combination” in most
cases.
Short of that, an option to tag the new file with a slightly different
file
name from the old file would enable the user to use an external “diff
editor” to compare and manually merge the two versions fairly
efficiently.
So, I’m wondering, does the Rails World contain any tools that can do
this?
~ Thanks yet again
~ Ken