Tuesday 14 July 2015

Choose the right call backs in rails. (an example by comparing after_create and after_commit)


 We get into the situation where incident object need to created/saved with before_save and after_create/after_commit.  
 first attempt [ incident object with before_save and after_create]  
 incident have an attribute called status.  
 before_save  
 def build_incident_logs  
    self.event_logs.build if status_changed?  
 end  
 incident object  
 incident.save  
 after_create  
 def update_attribute_of_incident  
    self.attribute_one = value    
    binding.pry  
    self.save!  
 end  
 a.Start the server begin the process, first it comes to before save and build the incident logs.  
 b.Then incident will be created.  
 c. After create is called and while trying to update attribute of incident, before save is called again. Now code inside before_save look for status changes.  
 4. All this happen inside same transaction.  
 5. So self.id_was nil. Again it will create an duplicate data in incident logs  
 I have attached the screen shot of this for better understand.  
 
Second attempt [ incident object with before_save and after_commit] Now change after_create to after_commit before_save def build_incident_logs self.event_logs.build if status_changed? end incident object incident.save after_commit def update_attribute_of_incident self.attribute_one = value binding.pry self.save! end a.Start the server begin the process, first it comes to before save and build the incident logs. b.Then incident will be created. c. After create is called and while trying to update attribute of incident, before save is called again. Now code inside before_save look for status changes. 4. Because of after_commit, it will start the new transaction and previous transaction was closed. 5. Status is available. 6. If i request for self.id_was some_id (eg:34). so no duplicate data is created at incident logs.
Conclusion: Above content will helpful to pick the right call backs based on requirement. after_create will look into same transaction but after_commit will begins from new transaction .

Friday 8 May 2015

What is new in rails4?

 Source : http://railscasts. Com/episodes/400-what-s-new-in-rails-4? View=asciicast  
 1. Concerns - http://stackoverflow. Com/questions/14541823/how-to-use-concerns-in-rails-4  
 2. Console-specific configuration here such as using pry  
 3. Postgress  
 rails 4 has support for native datatypes in postgres and we’ll show two of these here, although a lot more are supported: array and hstore. We can store arrays in a string-type column and specify the type for hstore.  
 4. New activerecord features  
 a. All method. This now returns an activerecord relation instead of an array of results which means that it’s now lazily loaded and only triggers a database query when it needs to. This also gets rid of the need for the scoped as it replaces this functionality  
 b. Load method - this will trigger the database query but return an activerecord relation instead of an array of results.  
 c. None method - which will return a activerecord relation with no records  
 d. Speaking of scopes, we can now call not on the where scope to invert it.  
 e. The order clause now accepts a hash so we can pass in a column name and the order that we want the results to be in. We can also pass in multiple column names to order by multiple columns.  
 f. Another useful new method is find_by. We’ve always been able to use dynamic finders, such as find_by_name, but these rely on method_missing and the implementation isn’t very clean. Rails 4 introduces a find_by method that we can pass a hash to, like this:  
 >> article. Find_by name: "hello"  
 5. Activemodel::model  
 6. Public/index. Html : this means that we no longer have to delete the public/index. Html  
 7. Gemfile changes  
 new default gem  
 1. Sprockets-rails  
 2. Turbolinks  
 8. Controller  
 a. Before_filter -> before_action  
 b. The next thing of note is that the update action now responds to either a put request or a patch request.  
 c. Update_attributes method-> update  
 d. Strong parameters built in replaces attr_accessible.  
 9. Scope syntex change  
 class article < activerecord::base  
  scope :sorted, order(:name)  
 end  
 class article < activerecord::base  
  scope :sorted, -> { order(:name) }  
 end  
 10. Changes to views  
 a. Lot of new helper methods  
 collection_select helper method aready exist  
 collection_check_boxes and collection_radio_buttons to generate lists of checkboxes or radio buttons for an association  
 there are also helper methods for the different html 5 input types.  
 b. Template handlers  
 c. Cache digest  
 11. Routes. Rb  
 concern  
 /config/routes. Rb  
 resources :articles do  
  resources :comments  
 endresources :photos do  
  resources :comments  
 end  
 ----------------------------  
 concern :commentable do  
  resources :comments    
 end  
 resources :articles, concerns: :commentable  
 resources :photos, concerns: :commentable  
 b. Match method depricated.  
 c.  
 get 'foo', to: 'articles#index', constraints: {protocol: "https", subdomain: "test"}  
 "https://test. Example. Com/foo"  
 12.  
 /config/environments/production. Rb  
 config. Eager_load = true  
 previously this behaviour was enabled using an option called threadsafe!  
 13. Testing  
 a. Instead of having unit and functional directories we have controllers, models and so on making it structured more like rspec is. Also, this now uses minitest.  
 b. This now has a different look and if a routing error is thrown it shows a list of the valid routes which is useful.  
 c. /rails/info path.  
 there were several things that were removed from rails 4 such as  
 activeresource, model observers, page and action caching and disabling rack::cache  
  by default. All of these can be brought back by using various gems if  
 you can’t live without them. One feature that we haven’t covered here is  
  actioncontroller::live. This will be covered in detail in a  
  future episode as it’s big enough to deserve one to itself. If you were  
  expecting a queueing api with asynchronous mailing support this has  
 been pulled from rails 4. 0 but should be included in rails 4. 1.