13. The model and scaffold generators will create migrations appropriate for adding a new model. Auto generated migrations
14. ruby script/generate model Product name:string description:text my_app/db/migrate/001_create_products.rb Creating a model
15. Migration generated class CreateProducts < ActiveRecord::Migration def self.up create_table : products do |t| t.string :name t.text :description t.timestamps end end def self.down drop_table :products end end
27. ActiveRecord is a Module ActiveRecord (note no space) could be classed as a module, since it's an implementation of the Active Record design pattern. (* need to ask)
54. Dynamic find methods person = Person.find_ by_fname ("mohit") all_mohit = Person.find_ all_by_fname ("mohit") person = Person.find_ by_fname_and_lname ("mohit",”jain”)
55. Creating a new record user = User.new user.name = "David" user.occupation = "Code Artist“ user .save OR user =User. new (:name => "David", :occupation => "Code Artist") user .save OR user =User .create (:name => "David", :occupation => "Code Artist")
56. Update a record person = Person.find(123) person .update_attribute(:name, "Barney" ) OR person= Person.find(123) person .name = "mohit” person.save OR person=Person. update(12, :name => "jigar" , :email => "jigar@vinsol.com" ) update_all and update_attribute bypass the validations.
57. Delete a record Person. delete (123) OR person = Person.find_by_name("Mohit") person. destroy
59. class Organization < ActiveRecord::Base has_many :people named_scope :active, :conditions => { :active => 'Yes' } end class Person < ActiveRecord::Base belongs_to :organization end Named scope
72. class Customer < ActiveRecord::Base end class Order < ActiveRecord::Base end OR class Customer < ActiveRecord::Base has_many :orders, :dependent => :destroy end class Order < ActiveRecord::Base belongs_to :customer end Comparison: without and with
81. has_and_belongs_to_many Stories can belong to many categories. Categories can have many stories. Categories_Stories Table story_id | category_id has_many through: -- gives you a third model Person can subscribe to many magazines. Magazines can have many subscribers. Subscriptions Table person_id | magazine_id | subscription_type | subscription_length | subscription_date has_and_belongs_to_many vs has_many :through
87. When Things Get Saved class Order < ActiveRecord::Base has_one :invoice end class Invoice < ActiveRecord::Base belongs_to :order end continue...
88. When things get saved (continue) If you assign an object to a has_one/has_many association in an existing object, that associated object will be automatically saved. order = Order.find(some_id) an_invoice = Invoice.new(...) order.invoice = an_invoice # invoice gets saved continue...
89. When things get saved (continue) If instead you assign a new object to a belongs_to association, it will never be automatically saved. order = Order.new(...) an_invoice.order = orde r # Order will not be saved here an_invoice.save # both the invoice and the order get saved
92. Example of validator helper class Person < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name, :on => :create, :message => "is already used" end
118. require 'test_helper' class ProductTest < ActiveSupport::TestCase # Replace this with your real tests. test " the truth " do assert true end end Test file?
121. validates_presence_of :title , :description , :image_url validates_numericality_of :price validate :price_must_be_at_least_a_cent validates_uniqueness_of :title validates_format_of :image_url, :with => %r{(gif|jpg|png)$}i, :message => 'must be a URL for GIF, JPG ' + 'or PNG image.' protected def price_must_be_at_least_a_cent errors.add(:price, 'should be at least 0.01' ) if price.nil? || price < 0.01 end Product Model
122. test " invalid with empty attributes " do product = Product.new assert !product.valid? assert product.errors. invalid?(:title) assert product.errors. invalid?(:description) assert product.errors. invalid?(:price) assert product.errors. invalid?(:image_url) end Test 1
123. Run the test case ruby -I test test/unit/product_test.rb #Loaded suite test/unit/product_test #Started #.. #Finished in 0.092314 seconds. 2 tests, 6 assertions, 0 failures, 0 errors
124. Test 2 test "positive price" do product = Product.new(:title => "My Book Title" , :description => "yyy" , :image_url => "zzz.jpg" ) product.price = -1 assert !product.valid? assert_equal "should be at least 0.01" , product.errors.on(:price) product.price = 0 assert !product.valid? assert_equal "should be at least 0.01" , product.errors.on(:price) product.price = 1 assert product.valid? end
126. ruby_book: title: Programming Ruby description: Dummy description price: 1234 image_url: ruby.png rails_book: title: Agile Web Development with Rails description: Dummy description price: 2345 image_url: rails.png YAML fixtures
127. Naming convention & usage fixtures :products Mention this in the ProductTest class :products => name of table and YML file
128. Fixture loading Loading involves three steps: * Remove any existing data * Load the fixture data * Dump the fixture data into a variable in case you want to access it directly
129. Test 3 (using fixture) test "unique title1" do product = Product.new(:title => products(:ruby_book).title , :description => "yyy" , :price => 1, :image_url => "fred.gif" ) assert !product.save