...filled out at the same time, there is no built-in validation. I've seen different solutions in the wild, each with different downsides: Private method referenced via validate: works...
...name, :nickname, xor_presence: true end Validator Here is the validator class; put it somewhere in your project. class XorPresenceValidator < ActiveModel::Validator def initialize(options) @attributes = Array.wrap(options[:attributes]).freeze...
The need for clearfix hacks has been greatly reduced since we could layout with Flexbox or CSS Grid. However, when you do need a clearfix, there's no reason to...
...hack anymore. You can just give the clearing container display: flow-root. This is supported by all browsers except IE11...
...check checkboxes. But there's one problem, if you want to test a custom styled checkbox, which hides its -Tag: The methods cannot (un)check checkboxes without an visible .
...error message will be something like: Unable to find visible checkbox "Some label" that is not disabled Solution 1 Use the keyword argument allow_label_click: true within the method...
RubyMine offers you to exclude directories from search, meaning faster search results and less "noise" in the list of result. Right-click a folder in your project tree and click...
...and other directories that you don't need to access during development and whose search results are irrelevant. They won't be deleted but simply ignored when searching across a...
...lambdas in Ruby work, check out the code at the attached link. Here the summary: ^ ---------------------------- Section 6: Summary ---------------------------- So, what's the final verdict on those 7 closure-like entities...
...method Y closure yes The things within each of these groups are all semantically identical -- that is, they're different syntaxes for the same thing: 1. block (called with yield...
Sometimes, the rails dev server doesn't terminate properly. This can for example happen when the dev server runs in a RubyMine terminal. When this happens, the old dev server...
...blocks port 3000, so when you try to start a new server, you get the error: Address already in use - bind(2) for "127.0.0.1" port 3000 (Errno::EADDRINUSE)
...is pretty useful, but manipulating values of arrays can be awkward because of its syntax. Consider the following users table which each example below will start from: name topics
Bob {llamas} (PostgreSQL uses curly braces to represent arrays, true story.) Adding values Use the array_cat function, or the || operator. These calls will add the values "cats...
...Ruby parser module of Ruby-GetText comes across a file in one of its search directories (e.g. lib/scripts/) and finds out that you are defining ActiveRecord classes inside it, it...
...the whole file. Here is how to avoid that. What's happening? Let's say you have the following script which is only run once, manually, via script/runner: # lib/scripts/doomsday.rb
...use Solr by calling Article.search { fulltext(title) paginate(:per_page => limit) } Now look at http://sunspot.github.com/docs/ to learn about all the possibilities of Sunspot. Overwrite Sunspots' search method
...your SysOp team that you'll need Solr on staging and production, on Ubuntu the solr-common will provide what you need. First of all you should provide Solr with...
A datetime in MySQL does not have a zone. It just stores the literal string "2010-05-01 12:00:00". That means that Rails must make...
...mode Rails assumes that your application lives in the same time zone as your server's local zone settings. In this mode ActiveRecord will not try to convert times coming...
ActiveRecord provides the ids method to pluck ids from a scope, but what if you need to pluck Global IDs? While you could just call map(&:to_global_id) on...
...your scope, this approach would instantiate each record just to do that. When you have many records, this will at the very least be slow. Here is a method that...
Sometimes it is useful to define a named scope by implementing a static method with the scope's name on the scoped class. For instance, when a method should decide...
...which existing scope should be the next link in the scope chain. Take this class for example: class Meal < ActiveRecord::Base named_scope :for_date, lambda { |date| :conditions => { :date => date...
If you want to to create maps within SASS/SCSS-files, it normally works like this: $some-map: (key1: value1, key2: value2) However, some maps can get big really fast, if they...
...since 2011 and it hasn't been resolved since then. Writing a map likes this: $some-map: ( key1: value1, key2: value2 ) confuses the SASS-parser and you will get an...
ActiveRecord offers an explain method similar to using EXPLAIN SQL statements on the database. However, this approach will explain all queries for the given scope which may include joins or...
Output will resemble your database's EXPLAIN style. For example, it looks like this on MySQL: User.where(id: 1).includes(:articles).explain EXPLAIN for: SELECT `users`.* FROM `users` WHERE...
SVG files are often much larger than necessary, containing comments, metadata, hidden elements etc. Optimize them with this tool. Web UI: https://jakearchibald.github.io/svgomg/ Binary: https://github.com/svg/svgo
...for a properly scaling SVG, you need to keep the viewBox attribute. There's an option --disable=removeViewBox for this...
When using the asset pipeline your assets (images, javascripts, stylesheets, fonts) live in folders inside app: app/assets/fonts app/assets/images app/assets/javascripts app/assets/stylesheets With the asset pipeline, you can use the...
When you deploy, Rails runs assets:precompile which precompiles all assets into static files that live in public/assets. This way you have all the performance of static files...
Ruby's standard library includes a class for creating temporary directories. Similar to Tempfile it creates a unique directory name. Note: You need to use a block or take care...
...of the cleanup manually You can create a prefix and suffix e.g. Dir.mktmpdir(['foo', 'bar']) => /tmp/foo20220912-14561-3g93n1bar You can choose a different base directory than Dir.tmpdir e.g. Dir.mktmpdir('foo', Rails.root.join('tmp...
...alias such as this to your ~/.bashrc: alias recent-branch="git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/ | fzf | sed 's/\* //g' | xargs -I '{}' git checkout {}"
Now whenever you want to switch back and forth between your most recent branches, type recent-branch, select one and press enter...
Middleman is a static page generator that brings many of the goodies that Rails developers are used to. Out of the box, Middleman brings Haml, Sass, helpers etc. However, it...
...these gems to the Gemfile: group :test do gem 'cucumber' gem 'spreewald' gem 'capybara' gem 'selenium-webdriver' # Only for running tests in a real browser (suggested) gem 'capybara-screenshot' # Optionally...
...you can install the official Java JRE: sudo apt-get update sudo apt-get install sun-java6-jre You can now check your default JVM with java -version. It should...
...versions below 10.10. If it does, please update this note. sudo add-apt-repository ppa:sun-java-community-team/sun-java6 You probably want to get rid of OpenJDK (which is...
...A minimum total number of forms (both blank and pre-filled) so the user sees more than just 2 blank Actor forms when she is entering Actors for the first...
...the number of total rows. By default there is no limit. You can also set defaults for built records by adding additional keys to the options hash: form.build_nested_records...
If you have an html_safe string, you won't be able to call gsub with a block and match reference variables like $1. They will be nil inside the...
...with rails_xss) as well as Rails 3 applications. Here is a fix to SafeBuffer#gsub. Note that it will only fix the $1 behavior, not give you a safe...
This is an extract from the linked article. It shows an approach on how to implement encrypted passwords with the AWS Key Management Service (KMS). For most applications it's...
...enough to use a hashed password with a salt (e.g. the gem devise defaults to this). Upon password creation Generate hash as hash of password + salt. Encrypt the hash with...
ActionMailer per default uses http as protocol, which enables SSL-stripping. When a logged-in user follows an http link to your application, it sends the cookies along with it...
...to send emails from public parts of your application with HTTP links and emails from SSL-protected parts with HTTPS, build a before_filter. If you need certain links with...