rails generate scaffold Post name:string title:string content:text
rails generate model Post title:string body:text published:boolean
rails generate migration AddFieldToModel field:type
:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
rails generate scaffold Post name:string title:string content:text slug:string:uniq
rails generate scaffold Post name:string title:string content:text slug:string:uniq
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.
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
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
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
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
Documentation
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
class Model....
alias_attribute :alias, :target_attribute
...
# One model
Dj> Model.objects.get(id=1)
Ra> Model.find(id: 1) # when using a PK
Ra> Model.find_by(name: "A name") # when using any other field
# Single record filtered collection
Dj> Model.objects.filter(name="First")
Ra> Model.where("name = First")
# Multiple record filtered collection
Dj> Model.objects.filter(name__startswith="First")
Ra> Model.where("name LIKE 'First%'")
Dj> Model.objects.all().order_by('created_at')
Ra> Model.all.order(created_at: :desc) # or 'created_at DESC'
Dj> Model.objects.all()[5:10]
Ra> Model.all.offset(5).limit(5)
Dj> Model.objects.all().first()
Ra> Model.first # and .second and .third, etc.
Dj> Model.objects.all().last()
Ra> Model.last
# Single array of fields
Dj> Model.objects.values_list('field', flat=True)
Ra> Model.pluck(:field)
# Multiple fields
Dj> Model.objects.values('field', 'otherfield')
Ra> Model.pluck(:field, :otherfield) # array of arrays
Ra> Model.select(:field, :otherfield) # array of partial objects
# Query spanning relations
Dj> Model.objects.filter(other_model__id=3)
Ra> Model.joins(:other_models).where("other_models.id = 3")
Dj> Model.objects.create(field="foo", other_field="bar")
Ra> Model.create(field: "foo", other_field: "bar")
Dj> Model.objects.get(5).update(field="bar")
Ra> Model.find(5).update(field: "bar")
or, less efficiently...
Dj> m = Model.objects.get(5)
Dj> m.field = "bar"
Dj> m.save()
Ra> m = Model.find(5)
Ra> m.field = "bar"
Ra> m.save
Dj> Model.objects.get(5).delete()
Ra> Model.find(5).destroy
Dj> Model.objects.filter(field="foo").update(field="bar")
Ra> Model.where(field: "foo").update_all(field: "bar")
Dj> Model.objects.all().delete()
Ra> Model.all.destroy_all