First, the class << foo syntax opens up foo's singleton class (eigenclass). This allows you to customize the behavior of methods called on that specific object. a = 'foo' class << a def inspect '"bar"' end end a.inspect # => "bar" a = 'foo' # new object, new singleton class a.inspect # => "foo" ow, … Continue reading class << self in Ruby
Ruby on Rails
define_method in Ruby
define_method is a (private) method of the object Class. You are calling it from an instance. define_method(:my_method) do |foo, bar| # or even |*args| # do something end or, you could even use string interpolation in the method name, such as define_method("#{attribute}=") do |unencrypted_password| if unencrypted_password.nil? self.send("#{attribute}_digest=", nil) elsif !unencrypted_password.empty? instance_variable_set("@#{attribute}", unencrypted_password) cost = ActiveModel::SecurePassword.min_cost … Continue reading define_method in Ruby
new update about has_secure_password in Rails 5.2
has_secure_password takes an attribute For many years has_secure_password only allowed a default password attribute. But now you can stash whatever you want in there. Source code Allows configurable attribute name for #has_secure_password. This still defaults to an attribute named 'password', causing no breaking change. Also includes a convenience method #<strong><span style="color:#008000;">regenerate_XXX</span></strong> where +XXX+ is the name … Continue reading new update about has_secure_password in Rails 5.2
How to Use link_to helper in Rails
Having documentation at hand Using documentation locally is helpful, especially if you can integrate it into your editor. Personally I’m using the ri docs with Emacs and Vim but I guess all those other editors can be configured to access it. If not you can always use ri in your terminal of choice. Another favourite … Continue reading How to Use link_to helper in Rails
What is gemfile in a Ruby or Rails application
Ruby developers use Gemfiles all the time, and most of us know how to do the basics. In this post I want to dive deep into everything we can do with a Gemfile. What is a Gemfile? A Gemfile is a file we create which is used for describing gem dependencies for Ruby programs. A … Continue reading What is gemfile in a Ruby or Rails application
HTTP Request in Rails
Request is an object in ActionDispatch (ActionDispatch::Request < Object ), which in turn is part of ActionPack. It is available in Rails helper (which is available in View). Controller is inherited from ActionController. The request object is typically only accessible from the controller and the view. You could just pass the request to the methods … Continue reading HTTP Request in Rails
Active Record Validations
Creating and saving a new record will send an SQL INSERT operation to the database. Updating an existing record will send an SQL UPDATE operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not perform … Continue reading Active Record Validations
Real-Time Rails: Implementing WebSockets in Rails 5 with Action Cable
Recent years have seen the rise of "the real-time web." Web apps we use every day rely on real-time features—the sort of features that let you see new posts magically appearing at the top of your feeds without having to lift a finger. While we may take those features for granted, they represent a significant … Continue reading Real-Time Rails: Implementing WebSockets in Rails 5 with Action Cable
helper_method in Rails
The method helper_method is to explicitly share some methods defined in the controller to make them available for the view. This is used for any method that you need to access from both controllers and helpers/views (standard helper methods are not available in controllers). e.g. common use case: #application_controller.rb def current_user @current_user ||= User.find_by_id!(session[:user_id]) end … Continue reading helper_method in Rails
Configure Rails on Windows 7
1. Go to https://rubyinstaller.org/downloads/ and download the recommended (not necessarily the latest version of the Ruby with Devkit to avoid potential gem compatibility issue (for example, sqlite3 on Windows platform) Which version to download? If you don’t know what version to install and you’re getting started with Ruby, we recommend you use Ruby+Devkit 2.4.X as … Continue reading Configure Rails on Windows 7