Last active
September 2, 2025 19:45
-
Star
(134)
You must be signed in to star a gist -
Fork
(31)
You must be signed in to fork a gist
-
-
Save cdesch/2f8de645cad1d83aa251c0a20b0f7097 to your computer and use it in GitHub Desktop.
Revisions
-
cdesch revised this gist
Jun 28, 2019 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,6 +16,11 @@ You can get all of this information on the command line. #### Generate Models rails generate model Post title:string body:text published:boolean #### Scaffold with ERB and API namespaced to /api/v1 rails g scaffold categories name:string image:string description:text status:boolean -c=scaffold_controller rails g scaffold_controller api/v1/categories name:string image:string description:text status:boolean --api --model-name=Category #### Add Column to Existing Model rails generate migration AddFieldToModel field:type -
cdesch revised this gist
Jun 28, 2019 . 1 changed file with 0 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,10 +22,6 @@ You can get all of this information on the command line. #### Column Types :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean #### Adding a Unique Property to a Field rails generate scaffold Post name:string title:string content:text slug:string:uniq -
cdesch revised this gist
Sep 26, 2017 . 1 changed file with 20 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,6 +30,26 @@ You can get all of this information on the command line. rails generate scaffold Post name:string title:string content:text slug:string:uniq #### Many to Many Relationship ([Reference](https://stackoverflow.com/questions/5322067/generating-a-model-with-many-to-many-in-ruby-on-rails)) Remember that you do not want an id for the join table, so make sure to add :id => false |t| ``` create_table assemblies_parts, :id => false do |t| t.integer :assembly_id t.integer :part_id end ``` If you use rails ``` rails generate model Assemblies_parts assembly:references part:references ``` you will have two indexes, but what you want is ``` add_index :assemblies_parts, [:assembly_id, :part_id], :unique => true ``` For Rails 5 use create_join_table instead. #### Adding Modifiers ([Reference](http://www.chrisjmendez.com/2016/07/01/rails-cheatsheet/)) Modifiers are inserted through curly braces and they allow you to include things such as null and limit. -
cdesch revised this gist
Sep 18, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -111,7 +111,7 @@ You can also manually update the migration file prior to running rake db:migrate add_column :tweet, :retweets_count, :integer, :null => false, :default => 0 ... and read [Rails API](http://api.rubyonrails.org/) ## Rspec Generators [Documentation](https://relishapp.com/rspec/rspec-rails/docs/generators) -
cdesch revised this gist
Sep 18, 2017 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -103,6 +103,16 @@ Suppose your building a collaborative app (like Pivotal Tracker and you want to rails g model Comment body:text commentable:references{polymorphic}:index ### Column Defaults ([Reference](https://stackoverflow.com/questions/6167994/assigning-default-value-while-creating-migration-file)) Default migration generator does not handle default values (column modifiers are supported but do not include default or null), but you could create your own generator. You can also manually update the migration file prior to running rake db:migrate by adding the options to add_column: add_column :tweet, :retweets_count, :integer, :null => false, :default => 0 ... and read Rails API ## Rspec Generators [Documentation](https://relishapp.com/rspec/rspec-rails/docs/generators) -
cdesch revised this gist
Sep 18, 2017 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ Cheat Sheets are greate but they are not a substitute for learning the framework and reading the documentation as we most certainly have not covered every potential example here. Please refer to the [Rails Command Line Docs](http://guides.rubyonrails.org/command_line.html) for more information. ## Command Line Generator Info [Reference](https://stackoverflow.com/questions/12900170/is-there-a-reference-cheat-sheet-for-rails-generate-command) -
cdesch revised this gist
Sep 18, 2017 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -95,6 +95,12 @@ class CreateProducts < ActiveRecord::Migration end ``` ### Polymorphism ([Reference](http://www.chrisjmendez.com/2016/07/01/rails-cheatsheet/)) Suppose your building a collaborative app (like Pivotal Tracker and you want to add comments to projects, tasks, and attachments. You can do that by making comments polymorphic. rails g model Comment body:text commentable:references{polymorphic}:index ## Rspec Generators [Documentation](https://relishapp.com/rspec/rspec-rails/docs/generators) -
cdesch revised this gist
Sep 18, 2017 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ You can get all of this information on the command line. rails generate scaffold Post name:string title:string content:text slug:string:uniq #### Adding Modifiers ([Reference](http://www.chrisjmendez.com/2016/07/01/rails-cheatsheet/)) Modifiers are inserted through curly braces and they allow you to include things such as null and limit. @@ -55,9 +55,8 @@ class CreateProducts < ActiveRecord::Migration end ``` #### Create a new model with a reference to another model ([Reference](https://www.ralfebert.de/snippets/ruby-rails/models-tables-migrations-cheat-sheet/)) rails g model Supplier name:string rails g model Product name:string:index sku:string{10}:uniq count:integer description:text supplier:references popularity:float 'price:decimal{10,2}' available:boolean availableSince:datetime image:binary -
cdesch revised this gist
Sep 18, 2017 . 1 changed file with 77 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,83 @@ You can get all of this information on the command line. #### Column Types :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean #### Adding a Unique Property to a Field rails generate scaffold Post name:string title:string content:text slug:string:uniq #### Adding a Unique Property to a Field rails generate scaffold Post name:string title:string content:text slug:string:uniq #### Adding Modifiers [Reference](http://www.chrisjmendez.com/2016/07/01/rails-cheatsheet/) Modifiers are inserted through curly braces and they allow you to include things such as null and limit. Add an age to a Friend with a limit rails g model friend age:integer{2} Add a price to a product with 2 decimals rails g model product 'price:decimal{10,2}' Would result in a migration with a `scale` of `2` and `percision` of `10` ``` class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| ... t.decimal :price, precision: 10, scale: 2 ... t.timestamps null: false end end end ``` #### Create a new model with a reference to another model [Reference](https://www.ralfebert.de/snippets/ruby-rails/models-tables-migrations-cheat-sheet/) rails g model Supplier name:string rails g model Product name:string:index sku:string{10}:uniq count:integer description:text supplier:references popularity:float 'price:decimal{10,2}' available:boolean availableSince:datetime image:binary Resulting migrations: ``` class CreateSuppliers < ActiveRecord::Migration def change create_table :suppliers do |t| t.string :name t.timestamps null: false end end end class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.string :name t.string :sku, limit: 10 t.integer :count t.text :description t.references :supplier, index: true, foreign_key: true t.float :popularity t.decimal :price, precision: 10, scale: 2 t.boolean :available t.datetime :availableSince t.binary :image t.timestamps null: false end add_index :products, :name add_index :products, :sku, unique: true end end ``` ## Rspec Generators [Documentation](https://relishapp.com/rspec/rspec-rails/docs/generators) rails generate rspec:model widget -
cdesch revised this gist
Sep 18, 2017 . 1 changed file with 21 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,24 @@ ## Command Line Generator Info [Reference](https://stackoverflow.com/questions/12900170/is-there-a-reference-cheat-sheet-for-rails-generate-command) You can get all of this information on the command line. `rails generate` with no generator name will output a list of all available generators and some information about global options. `rails generate GENERATOR --help` will list the options that can be passed to the specified generator. ## Rails Generate Examples #### Create a Resource rails generate scaffold Post name:string title:string content:text #### Generate Models rails generate model Post title:string body:text published:boolean #### Add Column to Existing Model rails generate migration AddFieldToModel field:type #### Column Types :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean ## Rspec Genorators [Documentation](https://relishapp.com/rspec/rspec-rails/docs/generators) -
cdesch created this gist
Sep 18, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ ## Scaffolding ## Rspec Genorators [Documentation](https://relishapp.com/rspec/rspec-rails/docs/generators) rails generate rspec:model widget will create a new spec file in `spec/models/widget_spec.rb` The same generator pattern is available for all specs: ``` scaffold model controller helper view mailer observer integration feature job ``` Generating specific views ``` rails g rspec:view widget index edit new show create spec/views/widget create spec/views/widget/index.html.erb_spec.rb create spec/views/widget/edit.html.erb_spec.rb create spec/views/widget/new.html.erb_spec.rb create spec/views/widget/show.html.erb_spec.rb ```