ruby on rails f.select options with custom attributes

Rails CAN add custom attributes to select options, using the existing options_for_select helper. You almost had it right in the code in your question. Using html5 data-attributes: <%= f.select :country_id, options_for_select( @countries.map{ |c| [c.name, c.id, {‘data-currency_code’=>c.currency_code}] }) %> Adding an initial selection: <%= f.select :country_id, options_for_select( @countries.map{ |c| [c.name, c.id, {‘data-currency_code’=>c.currency_code}] }, selected_key = f.object.country_id) … Read more

ruby/ruby on rails memory leak detection

Some tips to find memory leaks in Rails: use the Bleak House plugin implement Scout monitoring specifically the memory usage profiler try another simple memory usage logger The first is a graphical exploration of memory usage by objects in the ObjectSpace. The last two will help you identify specific usage patterns that are inflating memory … Read more

How to convert a ruby hash object to JSON?

One of the numerous niceties of Ruby is the possibility to extend existing classes with your own methods. That’s called “class reopening” or monkey-patching (the meaning of the latter can vary, though). So, take a look here: car = {:make => “bmw”, :year => “2003”} # => {:make=>”bmw”, :year=>”2003″} car.to_json # NoMethodError: undefined method `to_json’ … Read more

Ruby on Rails: Where to define global constants?

If your model is really “responsible” for the constants you should stick them there. You can create class methods to access them without creating a new object instance: class Card < ActiveRecord::Base def self.colours [‘white’, ‘blue’] end end # accessible like this Card.colours Alternatively, you can create class variables and an accessor. This is however … Read more

Difference between string and text in rails?

The difference relies in how the symbol is converted into its respective column type in query language. with MySQL :string is mapped to VARCHAR(255) https://edgeguides.rubyonrails.org/active_record_migrations.html :string | VARCHAR | :limit => 1 to 255 (default = 255) :text | TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536) Reference: https://hub.packtpub.com/working-rails-activerecord-migrations-models-scaffolding-and-database-completion/ … Read more

Float vs Decimal in ActiveRecord

I remember my CompSci professor saying never to use floats for currency. The reason for that is how the IEEE specification defines floats in binary format. Basically, it stores sign, fraction and exponent to represent a Float. It’s like a scientific notation for binary (something like +1.43*10^2). Because of that, it is impossible to store … Read more

Add a reference column migration in Rails 4

Rails 4.x When you already have users and uploads tables and wish to add a new relationship between them. All you need to do is: just generate a migration using the following command: rails g migration AddUserToUploads user:references Which will create a migration file as: class AddUserToUploads < ActiveRecord::Migration def change add_reference :uploads, :user, index: … Read more

How to configure Google Domains + Heroku w a Naked Domain

It’s advisable to make the www subdomain the default here, such that the root/naked domain redirects to www. This is in line with Heroku recommendations, which states: For maximum scalability and resiliency applications should avoid using DNS A-records and instead use a DNS provider that supports CNAME functionality at the apex, or use sub-domains exclusively … Read more