Posts

How to create rails project with specific rails version?

1) how to create rails project with specific rails version Option 1:  $ mkdir myapp $ cd myapp $ echo "source 'https://rubygems.org'" > Gemfile $ echo "gem 'rails', '5.0.0.1'" >> Gemfile $ bundle install $ bundle exec rails new . --force --skip-bundle If you check the entry for rails in  Gemfile , it should be like this: gem 'rails' , '~> 5.0.0' , '>= 5.0.0.1' You should update it to the exact version needed for the application: gem 'rails' , '5.0.0.1' Now, the final step: $ bundle update Option 2:  rails _4 .2 .7 .1 _ new MYAPP

Setup Rspec

 1 install following Gems       group :development, :test do          # Use for test application        gem 'rspec-rails', '~> 5.0.2'        # Use for fixtures        gem 'factory_bot_rails', '~> 5.2.0'        # For create fake data        gem 'faker', '~> 2.19.0'        # use assigns in controller rspec        gem 'rails-controller-testing'   end             bundle install       rails generate rspec:install       bundle exec rspec           2 Format Spec output on console      add below line in  spec/.rspec file (create new file .rspec)       --format documentation            3 Modal Test cases       rails g rspec:model user            4 controller Test cases      rails g rspec:controller users                   5 Use simplecov gem for check test case coverage code      https://github.com/colszowka/simplecov       OR       type following command       rails stats           6  factory_bot_rails       this gem is used for

Comparison between &&= and ||=

&&=    ||= Example 1 x=5 x=5 y=7 y=7 x&&=y x||=y X=7 x=5 y=7 y=7 Example 2 x=nil x=nil y=7 y=7 x&&=y x||=y x=nil x=5 y=7 y=7 Example 3 x=5 x=5 y=nil y=nil x&&=y x||=y x=nil x=5 y=nil y=nil

Difference between was, before_last_save, changed and changed?

  was: in the update action before update record if you want previous price not updated price then you should use @product.price_was before_last_save:   in the update action after update record if you want previous price not updated price then you should use @product.price_before_last_save changed?:   if you want to check your object is update or not? OR any value of object is updated or not then user this function.(Eg. @product.changed?) changed: if you want to check  what fields is updated  (Eg. @product.changed)

Standard of Code Sequence of model

 class Model < ActiveRecord::Base    #all mixins    include Something    extend Something     #other stuff    acts_as_taggable    paginates     #associations    has_many :something    belongs_to :something_else     #validations    validate_presence_of :something     #scopes    scope :something     #instance methods    def instance_method    end     #class methods    def self.method    end     #private methods    private    def method2    end End