Ruby – get the file being executed
Use methods in the File class to manipulate FILE Inside test.rb, I have puts __FILE__ puts File.dirname(__FILE__) puts File.basename(__FILE__) When you run, ruby a/b/test.rb You get a/b/test.rb a/b test.rb
Use methods in the File class to manipulate FILE Inside test.rb, I have puts __FILE__ puts File.dirname(__FILE__) puts File.basename(__FILE__) When you run, ruby a/b/test.rb You get a/b/test.rb a/b test.rb
You have to check which version of ActiveRecord you are using. According to your command rake db:migrate you are still on Ruby on Rails 4.2 or earlier. If you are on ActiveRecord up to 4.2 (change_column_default 4.2.9), there is no from/to option and you can define only the new default option as param. class ChangeDefaultvalueForHideSeasonSelector … Read more
Extract the attributes in to an array, assign them to a constant, then splat them in to attr_accessor. class SubClass < MyBaseClass ATTRS = [:id, :title, :body] attr_accessor(*ATTRS) end Now you can access them via the constant: puts SubClass.ATTRS #=> [:id, :title, :body]
To make it work with PostgreSQL version 12, I monkey patched PostgreSQLAdapter class to replace ‘panic’ with ‘warning’ message. Note, if you can upgrade activerecord gem to 4.2.6 or higher versions you don’t need to have this monkey patch. I had to do this because my project depends on gem activerecord-3.2.22.5 require ‘active_record/connection_adapters/postgresql_adapter’ class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter … Read more
If you have XCode 4 or later you will need to open it and go to Preferences -> Downloads -> Components and install the Command Line tools as they aren’t installed by default. Couldn’t install Rails until this happened.
Incase, while trying to update a bundle, you get an error “You have to install development tools first.” on OSX Mavericks (OSX 10.9), but you already have Xcode installed, you can still manually install the dev tools via terminal: xcode-select –install Do that and follow on-screen instructions. Your problem will be solved.
Rails 5 has redirect_back, instead of redirect_to :back. It was changed as it used to raise an exception when request’s HTTP_REFERER was not present. So use this: redirect_back fallback_location: root_path You can change root_path to something else as per your requirements.
There are many ways to do this. Your lack of context makes it impossible to elect a “best” way. Here’s a few ayways. Kernel.const_get(:Bob) eval(:Bob.to_s) Kernel.const_get(:bob.to_s.capitalize)
Here are some one-liner alternatives: # Ruby 2.1+ name_to_code = countries.map{ |c| [c.name,c.code] }.to_h # Ruby 1.8.7+ name_to_code = Hash[ countries.map{ |c| [c.name,c.code] } ] # Ruby 1.8.6+ name_to_code = Hash[ *countries.map{ |c| [c.name,c.code] }.flatten ] # Ruby 1.9+ name_to_code = {}.tap{ |h| countries.each{ |c| h[c.name] = c.code } } # Ruby 1.9+ name_to_code = … Read more
Time.at((date2.to_f – date1.to_f)*rand + date1.to_f) You’ll get a time object that is between two given datetimes.