Sunday, March 23, 2014

Ruby on Rails models

We generate a rails model only once, when we want to create a new table in the database. After that we use migrations to make changes to our tables.


rails generate model User name:string email:string
bundle exec rake db:migrate

bundle exec rake db:rollback

rails console                     # Saves to DB
rails console --sandbox    # Will not save to DB

>> User.new
>> user = User.new(name: "Test One", email: "asdf@asdf.com")     #Create in memory
>> user.save          # Write to DB, returns true on success
>> user.name
>> user.updated_at
>> user2 = User.create(name: "Test two", email: "asdf1@asdf.com")  # Create and write to DB
>> user2.destroy

>> User.find(1)
>> User.find_by(email: "asdf@asdf.com")

>> User.first
>> User.all

>> user.email = "test@one.com"
>> user.save
>> user.reload   # If you don't want to save your changes, and read details from DB

>> user.update_attributes(name: "Testing One", email: "qwer@qwer.com") # write to DB
>> user.update_attribute(:name, "Tester One")

rake test:prepare

rails generate migration add_password_digest_to_users password_digest:string

2 comments:

21cssIndia said...

Course contents
http://www.21cssindia.com/courses/ruby-on-rails-online-training-142.html
Database - basic - HTML / CSS - Caching & Routing - ROR-Architecture - HTML / CSS - More about Ruby -Ruby oops -ROR Validations & callbacks - XHTML/CSS - Ajax - Ruby oops - SVN -DB- Design - Employees to learn at their own pace and maintain control of learning “where, when and how” with boundless access 24/7by 21st Century Software Solutions. contact@21cssindia.com ---- Call Us +919000444287

comettech said...

Thanks for information,we are providing http://www.keentechnologies.com/sap-crm_technical-online-training.html.For more details visit our website.