Monday, September 04, 2017

Rails 5 makes belongs_to association required by default


In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default. This change will have some impact if you are migrating an existing Rails 4 application in to Rails 5 and foreign key is not mandatory in your application. It will trigger validation error if associated record is not present.

For example, if your User model has city_id as foreign key, then if you create user without adding city_id then rails 5 will throw validation error.



class User < ApplicationRecord
  belongs_to :city
end


How to achieve this behavior before Rails 5

In Rails 4.x world in order to add validation on belongs_to association, we need to add option required: true.  By default is false


class User < ApplicationRecord
  belongs_to :city, required: true
end


Turn off this default behavior in Rails 5

If we want to turn off this behavior we can use :optional => true . When set to true, the association will not have its presence validated.


class User < ApplicationRecord
  belongs_to :city, optional: true
end

You can turn off this in entire application by setting


Rails.application.config.active_record.belongs_to_required_by_default = false




Thursday, August 17, 2017

Rails 5: Autoloading and Reloading Constants



Rails 5 disables autoloading after booting the app in production. With Rails 5, autoloading is now completely disabled if config.eager_load = true.

The concept of loading all the constants even before they are actually needed is called “Eager loading”. In a way it is opposite of “Autoloading”. In the case of “Autoloading” the application does not load the constant until it is needed. Once a class is needed and it is missing then the application starts looking in “autoloading paths” to load the missing class.

When application boots in production then the application loads all constants found in all directories listed in eager_load_paths. If folder in the autoload_paths but not in the eager_load_paths are not going to be loaded in production. What this means is that if file paths outside of app/ are in your config.autoload_paths and not in config.eager_load_paths (for example, lib/), these files will no longer load in deployed environments.


In my case I had a file called calendar.rb in the lib folder.

class Calendar < Struct.new(:view, :date, :callback)
...
end


and I was trying to use this class in a helper:


module CalendarHelper

  include ActionView::Helpers::OutputSafetyHelper

  include ActionView::Helpers::TagHelper

  include ActionView::Context
  def calendar(date = Date.current, &block)
    Calendar.new(self, date,  block).display
  end

...
end 


In my application.rb I had next configuration:

config.autoload_paths += %W(#{config.root}/lib)

But when I was trying to call Calendar.new from my helper, I was getting an error. Strange errors regarding missing requires and dependencies.


ActionView::Template::Error (uninitialized constant CalendarHelper::Calendar):

So what I had to do was change the configuration

# config/application.rb
config.eager_load_paths << Rails.root.join('lib')

And now everything works fine


Tuesday, August 08, 2017

How to connect the localhost Rails site via Mobile locally


If you want to test your app locally in a mobile or tablet, without the need to have a server, you can try ngrok.  Ngrok is a very cool, lightweight tool that creates a secure tunnel on your local machine along with a public URL you can use for browsing your local site.

It is very simple to use. Follow these steps:


Step 1

Download ngrok and install it.



Step 2

Run your Rails app:
$ rails s


Step 3

Go to the directory where you extracted the zip file and run it.

$  ./ngrok http 3000



Step 4
You can now access the Rails app from the URL displayed in the ngrok output. It will be something like: http://2ec640eb.ngrok.io


Tuesday, April 04, 2017

Heroku: Deploying with GIT



In this post I will explain how to deploy to Heroku directly through GIT. In order to see what git tree your Heroku repo is using, try:

$ git ls-remote git@heroku.com:APP-NAME.git

To find out the Heroku git repository name, you can also try

$ heroku info --app APP-NAME
=== app-name
Addons:
Auto Cert Mgmt:
Collaborators:
Dynos:
Git URL:
...

Right now, you know the Heroku git repository name, but you don't have a remote branch to push to.
Your Heroku app starts with a blank repository, it has no branches and no code. So the first time you deploy, you’ll need to specify a remote branch to push to.

For instance, imagine you have a server for Production where you deploy the APP-NAME, so you can create the remote called "pro-heroku".


$ git remote add pro-heroku https://git.heroku.com/APP-NAME.git

And now, if you have the git branch called "production" and we need to deploy this branch to Production, then you can try:

$ git push pro-heroku production:master

This will push your local branch production to the remote branch master in pro-heroku so APP-NAME will get deployed with the code in production branch.


I hope this post helps, if you have any comment or something please don't hesitate to add your comments.