Subtraction of ruby ​​hash

I have 2 hashes (that later will be converted to array, not sure if this is relevant): Hash1 = {"X"=>2, "Y"=>1, "Z"=>1} Hash2 = {"X"=>1, "Y"=>1} I need to subtract them like (Hash3 = Hash1 -

Rails Group by inside Group by

I need to group my ActiveRecords first by YEAR and then by MONTH, something like this: {2017=> "January"=> [Record1, record2, etc] "February"=> [Record1, record2, etc] 2016=> "January"=> [Record1, record2, etc]

Creating rows in a table associated with 2 others

Have models: class Usdzar < ActiveRecord::Base has_many :smoothings has_many :smoothvals, through: :smoothings end class Smoothval < ActiveRecord::Base has_many :smoothprices has_many :usdzars, through: :smoothings end class Smoothing < ActiveRec

If the instruction syntax error, unexpected keyword_end

I'm doing a exercise on hackerrank where I'm asked to compare a set of triplets to get a score. After my initial submission I wanted to try something more elegant (that is to say with less conditionals) so I made the following code: def solve(a0, a1,

rake db: seed not including all my data

I am having issues of my rake db:seed not being applied to the correct values when I attempt to seed. I also get no error when I rake db:seed which is why I am unsure what is wrong. Here are the migration files: Boostdets: class CreateBoostdets < Act

Best way to send () optional parameter in Rails

I have a method which accepts two parameters. One of them is optional. These params are actually names of methods which should be send to an object. How to avoid getting NoMethodError if the second parameter is missed without using condition? Can I s

Custom validation rails involving current_user

I have been developing a rails app in which we allow different users to submit different values for an attribute of a model. For example all users can submit values greater than 10 while administrators are allowed to submit one digit values as well.

Ways to refactor named arguments with default values

I have a method with a lot of named arguments, some with default values: def myClass def initialize(a:, b:, c:, d:, e:, f:, g: nil, h: nil, i: nil) ... end end The list is a little hard to look at and comprehend. I am looking for way to make this sim

Delete a comment if the user does not exist Ruby on Rails

I get an error undefined method 'email' for nil:NilClass when I try to access a post with a comment from a user that has been deleted in the DB. I wonder: how can I remove the comments that has been created by users who no longer exists "on the fly&q

Error installing Rubygems 'rhc' on Windows

I'm trying to install and configure 'OpenShift' on Windows. I installed the necessary tools like: Git, RubyGem 2.x, but when I try to install 'rhc' ruby gem I got this error: C:\>ruby -v ruby 2.1.6p336 (2015-04-13 revision 50298) [x64-mingw32] C:\>g

How to run an RSpec test on a specific line?

I have next spec file: require 'rails_helper' describe Order do it "calculates the total price of the order" do item1 = create(:item) item2 = create(:item, price: 20) order = create(:order) order.items << item1 order.items << item2 o

Analyze a string with regex using ordered delimiters

I have a string of the following format: University/Class (Term) I need to split it into its three parts. I came up with /\/|\s\(|\)/g and that works really well for simple cases. Unfortunately, each of the three parts can contain those three delimit

the rails reach can not generate a correct SQL

I added a variable in config/application.rb: config.available_account_types = %w(SystemAccount CashAccount DemandAccount LiabilityAccount CreditCardAccount) And generated some scopes in model account.rb: for t in Mypurse::Application.config.available

Count the keys redis without looking for them in Ruby

I'm keeping a list of online users in Redis with one key corresponding to one user. Keys are set to time out in 15 minutes, so all I have to do to see how many users have roughly been active in the past 15 minutes, I can do: redisCli.keys('user:*').c

Custom validation of Rails with request Params?

Rails noob , I am trying to write a custom validation but I am dependent on parameters that come from the post request. This example is similar to what I have - in this post model I want to validate that a user didn't already post on some topic, but

Need to know about CMS

In Ruby On Rails which CMS gem support the more powerful features and fully customizable functionality. I need to create an application that have an admin that create,update and edit, delete the pages. And wants to use customizable CSS and additional

Chruby install Ruby 1.8.7

I'm using chruby for version management and am cloning a project that is unfortunately in Ruby 1.8.7. Is it possible to install Ruby 1.8.7 with chruby? I'm having trouble finding resources for this process.We did remove all workaround patches and 1.8

Running the Rails module from the command prompt

I have a module like this in my Rails application. I have a folder extras inside my app folder. Inside that i have a module. module ExcelUpload class Eupload def do_something end end def self.excel_upload upload_service = Eupload.new upload_service.d

How to reference global variables and class variables?

I'm new to programming. Right now I'm studying Ruby. To my understanding, global variables are defined in the global namespace (so outside of any classes or functions). I'm reading something and it says global variables have a $ sign before them. Wha

Best way to randomize (Ruby / Rails 3)

This question already has an answer here: Rails select random record 7 answers I'm currently using: @users = User.order("RANDOM()").limit(6) to generate a list of 6 random users - however, this method takes a 1200ms toll on page load times. Is t

Javascript & ldquo; development & rdquo; and "production

I'm developing a simple application in Sinatra that makes many asynchronous calls. In my javascript code I have a variable that stores my domain for all calls. My problem is how to alter the value of the variable every time that I publish my applicat

Ruby + ActiveRecord: Reading from the database

I've written a Ruby script that parses a tab-delimited text file and writes the output to a sqlite3 database with ActiveRecord. Next, I need to write another Ruby script to display the database's contents as static HTML. The text file that I'm workin

Implicit Tables in Ruby on Rails

I'm just learning how to program in Ruby on Rails, and I'm really impressed by how much work the Ruby on Rails framework does for you. I have the following two classes: Object -------- object_id description ObjectGroup -------- group_id description T