Skip to content
This repository
Browse code

[engines guide] Add information about running and reverting specified…

… migrations [skip ci]
  • Loading branch information...
commit 5520e36e48f5df3c8c92f0d2eea9bbe60e079fdf 1 parent 7950622
Piotr Sarnacki drogus authored

Showing 1 changed file with 16 additions and 4 deletions. Show diff stats Hide diff stats

  1. +16 4 railties/guides/source/engines.textile
20 railties/guides/source/engines.textile
Source Rendered
@@ -315,7 +315,7 @@ resources :posts do
315 315 end
316 316 </ruby>
317 317
318   -This creates a nested route for the comments, which is what the form requires.
  318 +This creates a nested route for the comments, which is what the form requires.
319 319
320 320 The route now exists, but the controller that this route goes to does not. To create it, run this command:
321 321
@@ -361,7 +361,7 @@ This is the final part required to get the new comment form working. Displaying
361 361 * "/Users/ryan/Sites/side_projects/blorgh/app/views"
362 362 </text>
363 363
364   -The engine is unable to find the partial required for rendering the comments. Rails has looked firstly in the application's (+test/dummy+) +app/views+ directory and then in the engine's +app/views+ directory. When it can't find it, it will throw this error. The engine knows to look for +blorgh/comments/comment+ because the model object it is receiving is from the +Blorgh::Comment+ class.
  364 +The engine is unable to find the partial required for rendering the comments. Rails has looked firstly in the application's (+test/dummy+) +app/views+ directory and then in the engine's +app/views+ directory. When it can't find it, it will throw this error. The engine knows to look for +blorgh/comments/comment+ because the model object it is receiving is from the +Blorgh::Comment+ class.
365 365
366 366 This partial will be responsible for rendering just the comment text, for now. Create a new file at +app/views/blorgh/comments/_comment.html.erb+ and put this line inside it:
367 367
@@ -440,6 +440,18 @@ The first timestamp (+\[timestamp_1\]+) will be the current time and the second
440 440
441 441 To run these migrations within the context of the application, simply run +rake db:migrate+. When accessing the engine through +http://localhost:3000/blog+, the posts will be empty. This is because the table created inside the application is different from the one created within the engine. Go ahead, play around with the newly mounted engine. You'll find that it's the same as when it was only an engine.
442 442
  443 +If you would like to run migrations only from one engine, you can do it by specifying +SCOPE+:
  444 +
  445 +<shell>
  446 +rake db:migrate SCOPE=blorgh
  447 +</shell>
  448 +
  449 +This may be useful if you want to revert engine's migrations before removing it. In order to revert all migrations from blorgh engine you can run such code:
  450 +
  451 +<shell>
  452 +rake db:migrate SCOPE=blorgh VERSION=0
  453 +</shell>
  454 +
443 455 h4. Using a class provided by the application
444 456
445 457 When an engine is created, it may want to use specific classes from an application to provide links between the pieces of the engine and the pieces of the application. In the case of the +blorgh+ engine, making posts and comments have authors would make a lot of sense.
@@ -481,12 +493,12 @@ private
481 493 end
482 494 </ruby>
483 495
484   -By defining that the +author+ association's object is represented by the +User+ class a link is established between the engine and the application. There needs to be a way of associating the records in the +blorgh_posts+ table with the records in the +users+ table. Because the association is called +author+, there should be an +author_id+ column added to the +blorgh_posts+ table.
  496 +By defining that the +author+ association's object is represented by the +User+ class a link is established between the engine and the application. There needs to be a way of associating the records in the +blorgh_posts+ table with the records in the +users+ table. Because the association is called +author+, there should be an +author_id+ column added to the +blorgh_posts+ table.
485 497
486 498 To generate this new column, run this command within the engine:
487 499
488 500 <shell>
489   -$ rails g migration add_author_id_to_blorgh_posts author_id:integer
  501 +$ rails g migration add_author_id_to_blorgh_posts author_id:integer
490 502 </shell>
491 503
492 504 NOTE: Due to the migration's name and the column specification after it, Rails will automatically know that you want to add a column to a specific table and write that into the migration for you. You don't need to tell it any more than this.

0 comments on commit 5520e36

Please sign in to comment.
Something went wrong with that request. Please try again.