setting up db with rails console
rails generate draft:resource <MODEL_NAME_SINGULAR> <COLUMN_1_NAME>:<COLUMN_1_DATATYPE> <COLUMN_2_NAME>:<COLUMN_2_DATATYPE> # etc
to generate a complete, database-backed web resource, use the code snippet above. It is actually exactly the same as creating and generating a model and a table, but instead model is replaced with draft:resource
. below is an example of using this.
rails generate draft:resource post title:string body:text expires_on:date board_id:integer
or...in our assignment here...
rails generate draft:resource board name:string
this action that we just did has essentially installed all of our standard CRUD action boilerplate for us (navigate to /posts
. if you would like to delete the action you just made, you can also rails destroy draft:resource
, rake db:rollback
, rails destroy draft:resource post
or go back to the last git commit. you can then correct your error and then regenerate.
adding and removing columns
To modify existing tables, the most common tools we use are
add_column
andremove_column
.generate a new migration (not a whole model like above) at a bash prompt with, for example, use
rails g migration AddImageToPosts
, orrails g migration RemoveExpiresOnFromPosts
then, go into the new migration file and add instructions to make the
change
you want within the change method (or theup
method, if that’s what you find inside instead of change)
def change
add_column :posts, :image, :string
end
- with ultimate last resort, use
rake db:drop
other notes
- rspec rails framework
bundle exec rspec
or rspec
to run rails debugging with ruby gem inside the GemFile
Top comments (0)