FAQ Database Discussion Community

## Error when trying to install app with mysql2 gem

mysql,ruby-on-rails,ruby,ruby-on-rails-4
Im trying to install an open source rails 3.2.21 application that uses the mysql2 gem, but when i try and run the bundle commant I get the following error: Fetching: mysql2-0.3.18.gem (100%) Building native extensions. This could take a while... p ERROR: Error installing mysql2: ERROR: Failed to build gem...

## Does rails have an opposite of parameterize for strings?

ruby-on-rails,ruby
I used parameterize method. I want to de-parameterize it. Is there a method to do the opposite of parameterize?

## Couldn't find User with 'id'=show

ruby-on-rails,ruby,devise
a Rails/Devise Question for you today. I'm working on user profiles for a site and I am hitting this error ActiveRecord::RecordNotFound at /users/show Couldn't find User with 'id'=show Not sure exactly what's happening as I haven't really run into anything like this before. Here are some necessary files to take...

## Same enum values for multiple columns

ruby-on-rails,ruby,enums
I need to do something like this: class PlanetEdge < ActiveRecord::Base enum :first_planet [ :earth, :mars, :jupiter] enum :second_planet [ :earth, :mars, :jupiter] end Where my table is a table of edges but each vertex is an integer. However, it seems the abvove is not possible in rails. What might...

## Make instance variable accessible through hash in Ruby

ruby-on-rails,ruby,ruby-on-rails-4,activerecord
In Rails, ActiveRecord objects, attributes are accessible via method as well as through Hash. Example: user = User.first # Assuming User to be inheriting from ActiveRecord::Base user.name # Accessing attribute 'name' via method user[:name] # Attribute 'name' is accessible via hash as well How to make instance variables accessible through...

## Allowing some enabled and disabled option on collection_select

ruby-on-rails,ruby
I am trying to populate a dropdown box on a view that has all the states. This works just fine: <%= f.collection_select :state_id, @states, :id, :name %> Now, I need to make the following: Some states are going to be disabled for choosing, but they still have to appear on...

## Map with accumulator on an array

ruby,inject
I'm looking to create a method for Enumerable that does map and inject at the same time. For example, calling it map_with_accumulator, [1,2,3,4].map_with_accumulator(:+) # => [1, 3, 6, 10] or for strings ['a','b','c','d'].map_with_accumulator {|acc,el| acc + '_' + el} # => ['a','a_b','a_b_c','a_b_c_d'] I fail to get a solution working. I...

ruby-on-rails,ruby,path,user,friendly-id
To show User's profile I used their id. In controller: @user = User.find(params[:id]) In routes: get ':id' => 'users#show' But now I try new way with username: get ':username' => 'users#show' #route @user = User.find_by(username: params[:username]) #controller And when I go to user's profile with /username I see error: undefined...

## Batch file to open multiple instances of cmd and run Ruby script in each instance

ruby,windows,batch-file,cmd,watir-webdriver
I'm trying to open multiple instances of cmd up which each run a ruby script at the same time. For background into the scripts, each of these ruby scripts run watir-webdriver and spawn new browser instances. All of this should happen instantaneously. Here's what I've tried: start cmd /k cd...

## Seeding fails validation for nested tables (validates_presence_of)

ruby-on-rails,ruby,validation,ruby-on-rails-4,associations
An Organization model has a 1:many association with a User model. I have the following validation in my User model file: belongs_to :organization validates_presence_of :organization_id, :unless => 'usertype==1' If usertype is 1, it means the user will have no organization associated to it. For a different usertype the presence of...

## Recursive solution doesn't iterate correctly

ruby,algorithm,search,recursion
I'm working through a toy problem in Ruby: how to produce all possible 10-digit phone numbers where each successive number is adjacent to the last on the keypad. I've represented the adjacent relationships between numbers, and have a recursive function, but my method isn't iterating through the whole solution space....

## In Ruby how to put multiple lines in one guard clause?

ruby-on-rails,ruby
I have the following line of code : if params[:"available_#{district.id}"] == 'true' @deliverycharge = @product.deliverycharges.create!(districtrate_id: district.id) delivery_custom_price(district) end Rubocop highlight it and asks me to use a guard clause for it. How can I do it? EDIT : Rubocop highlighted the first line and gave this message Use a guard...

## is there an equivalent of the ruby any method in javascript?

javascript,arrays,ruby,iteration
Is there an equivalent of ruby's any method for arrays but in javascript? I'm looking for something like this: arr = ['foo','bar','fizz', 'buzz'] arr.any? { |w| w.include? 'z' } #=> true I can get a similar effect with javascript's forEach method but it requires iterating through the entire array rather...

## Keep leading zeroes when converting string to integer

ruby
For no particular reason, I am trying to add a #reverse method to the Integer class: class Integer def reverse self.to_s.reverse.to_i end end puts 1337.reverse # => 7331 puts 1000.reverse # => 1 This works fine except for numbers ending in a 0, as shown when 1000.reverse returns 1 rather...

## Installing GitLab CI Runner on Raspberry Pi 2 (Raspbian)

ruby,gitlab,raspberry-pi2
I want to install the GitLab Runner for CI on my RPI 2 machine running Raspbian. There is no armhf package available or mentioned on the official page: https://gitlab.com/gitlab-org/omnibus-gitlab-runner/blob/master/doc/install/README.md and I could not find one on the net. I've tried building it from source but it failed to make ruby...

## Get an array including every 14th day from a given date

ruby
In ruby, how can I get every 14th day of the year, going backwards and forwards from a date. So consider I'm billed for 2 weeks of recycling on today, 6-16-2015. How can I get an array of every recycling billing day this year based on that date. ...

## Having trouble calling a Proc within a Method within a Class

ruby,proc
I am struggling to debug my issue; I am trying to use a Proc within a method, all inside a class. When I don't type in the "class" details, my program compiles fine; when I specify the class name, however, I get a variety of different errors. Note: I posted...

## Ruby Dir.glob mystery: Where are the FNM_xxx flags described?

ruby
Note: This has already been posted on Ruby Forum some weeks ago. I'm crossposting it here, because I didn't get any response so far Dir.glob provides an optional parameter, usually referred as 'flags'. Where can I find a documentation about what flags are possible? The Ruby 2.0 docs just say...

## Appending an element to a page in VoltRb

html,ruby,opalrb,voltrb

## Rails less url path change

ruby-on-rails,ruby,url,path,less
Developing a Rails application with the less-rails gem I found something unusual : // app/assets/common/css/desktop/typo.less @font-face{ font-family:'SomeFont'; src:url("fonts/db92e416-da16-4ae2-a4c9-378dc24b7952.eot?#iefix"); // ... } The requested font is app/assets/common/css/fonts/db92e416-da16-4ae2-a4c9-378dc24b7952.eot This font is compiled with less and the results is : @font-face { font-family: 'SomeFont'; src: url("desktop/fonts/db92e416-da16-4ae2-a4c9-378dc24b7952.eot?#iefix"); //... } Do you know why is...

## How could I padding spaces to a fix length

ruby
I need all strings' length with 5 Original [477, 4770,] Expected ["477 ", "4770 "] How could I do it with Ruby ?...

## How to pivot array into another array in Ruby

arrays,ruby,csv
I have a multidimensional array like this one : myArray = [["Alaska","Rain","3"],["Alaska","Snow","4"],["Alabama","Snow","2"],["Alabama","Hail","1"]] I would like to end up with CSV output like this. State,Snow,Rain,Hail Alaska,4,3,nil Alabama,2,nil,1 I know that to get this outputted to CSV the way I want it I have to have output array like this: outputArray =[["State","Snow","Rain","Hail"],["Alaska",4,3,nil],["Alabama",2,nil,1]]...

## Rspec: Factory Girl & Complex Associations

ruby-on-rails,ruby,rspec,tdd,factory-girl
I am trying to test a helper method that would return the amount of Subscribers a Campaign is targeting. Being fairly new to TDD, I am having a hard time wrapping my head around how to write this one since the associations are quite complex. A Campaign "targets" by whether...

## Using attr_accessor from inside class? [duplicate]

ruby,attr-accessor
This question already has an answer here: Why do Ruby setters need “self.” qualification within the class? 3 answers I'm trying to use an attr_accessor from inside the class where it is defined, to no avail. Why does this not work? I would expect the following to output "new"...

## How to flatten a structure of embedded Set and Hash

ruby,recursion
I would like to convert an embedding structure into a flat one. An embedding structure is a set of 0 or more objects, such as: a string or a hash having some string as key and some other embedding structure as value. A flat structure is a set of arrays...

## Prevent upvote model from being called for every comment

ruby-on-rails,ruby,database,performance,model
I have three models: User, Comment and Upvote. User-to-Comment has a one-to-many relation, Comment-to-Upvote has a one-to-many relation and User-to-Upvote has a one-to-many relation. I want to do something similar to the upvoting done on Stackoverflow. So when you upvote/downvote the arrow will highlight and remain highlighted even if you...

## RSpec test for rake task

I have created a custom rake task that deletes all items that are >= 7 days old. I am trying to write a RSpec test for this new task but it seems like my task isn't really running in the test. I have tested the task manually and it works...

ruby

## No Method When Importing to Rails via CSV

ruby-on-rails,ruby
I'm importing into my Rails App via CSV. The import worked until I tried to create a new contact using one of the columns on my CSV. Here's the code that imports the data: row = Hash[[header, spreadsheet.row(i)].transpose] hotel = find_by_id(row["id"]) || new hotel.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k...