User Tools

Site Tools


ruby

This is an old revision of the document!


Ruby

Rails

Popular Gems

Seed Data

bundle add faker  # Add it to only the development and test groups, if you are a stickler for convention

# In db/seeds.rb
50.times do
  MyModel.create! name: Faker::Name.name
end

rails db:migrate && rails db:seed

Scaffolding

rails g scaffold MyModel field1:string

Data Types:

  • binary
  • boolean
  • date
  • datetime
  • decimal
  • float
  • integer
  • primary_key
  • string
  • text
  • time
  • timestamp
  • references

Example for references:

rails g scaffold Country name:string
rails g scaffold City name:string country:references

Multiple References

To create multiple references to another model (for different attribute fields):

# The other model
rails g Country name

# In the scaffold command
rails g scaffold Staff residing_country:references

# In the migration
t.references :residing_country, null: false, foreign_key: { to_table: :countries }

# In the model
belongs_to :residing_country, class_name: 'Country', foreign_key: 'residing_country_id'

Resetting the Database

rm storage/development.sqlite db/schema.rb
rails db:drop db:create db:migrate
ruby.1773503236.txt.gz · Last modified: by reddy

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki