Rails Migrations: tried to change the type of column from string to integer

I quote the manual about ALTER TABLE: A USING clause must be provided if there is no implicit or assignment cast from old to new type. What you need is: ALTER TABLE listings ALTER longitude TYPE integer USING longitude::int; ALTER TABLE listings ALTER latitude TYPE integer USING latitude::int; Or shorter and faster (for big tables) … Read more

All factors of a given number

In Ruby, the prime library gives you the factorization: require ‘prime’ 4800.prime_division #=> [[2, 6], [3, 1], [5, 2]] To get that list of yours, you take the cartesian product of the possible powers: require ‘prime’ def factors_of(number) primes, powers = number.prime_division.transpose exponents = powers.map{|i| (0..i).to_a} divisors = exponents.shift.product(*exponents).map do |powers| primes.zip(powers).map{|prime, power| prime ** … Read more

Iterate over Ruby Time object with delta

Prior to 1.9, you could use Range#step: (start_time..end_time).step(3600) do |hour| # … end However, this strategy is quite slow since it would call Time#succ 3600 times. Instead, as pointed out by dolzenko in his answer, a more efficient solution is to use a simple loop: hour = start_time while hour < end_time # … hour … Read more

Getting a “bad interpreter” error when using brew

I got this error (much the same): /usr/local/bin/brew: /usr/local/Library/brew.rb: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby: bad interpreter: No such file or directory /usr/local/bin/brew: line 26: /usr/local/Library/brew.rb: Undefined error: 0 and fixed by the solution below: Open brew.rb: $ sudo vim /usr/local/Library/brew.rb Change the first line’s 1.8 to Current: Before: #!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -W0 After: #!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby -W0 Then brew works for me. Hope … Read more

How do I parse an HTML table with Nokogiri?

#!/usr/bin/ruby1.8 require ‘nokogiri’ require ‘pp’ html = <<-EOS (The HTML from the question goes here) EOS doc = Nokogiri::HTML(html) rows = doc.xpath(‘//table/tbody[@id=”threadbits_forum_251″]/tr’) details = rows.collect do |row| detail = {} [ [:title, ‘td[3]/div[1]/a/text()’], [:name, ‘td[3]/div[2]/span/a/text()’], [:date, ‘td[4]/text()’], [:time, ‘td[4]/span/text()’], [:number, ‘td[5]/a/text()’], [:views, ‘td[6]/text()’], ].each do |name, xpath| detail[name] = row.at_xpath(xpath).to_s.strip end detail end pp details … Read more

How to use “RVM –default” on MacOSX

I had the same problem once. It turned out the rvm-script got loaded twice, which broke things a bit. Check all the files that load when you open a shell: /etc/profile ~/.bashrc ~/.bash_profile and so on, and make sure they don’t load RVM twice. Maybe put echo “Going to load RVM” before [[ -s “/Users/user/.rvm/scripts/rvm” … Read more

Can’t install pg gem on Windows

The message you’re getting is a clear indication that you lack something for the correct installation of that gem: Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options. There is no Windows native version of latest release … Read more

Why isn’t current directory on my Ruby path? [duplicate]

In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about require_relative. My apps tend to look like this: require ‘some_gem’ require ‘another_gem’ require_relative ‘lib/init’ And then lib/init.rb can … Read more

Rails 4.0 Strong Parameters nested attributes with a key that points to a hash

My other answer was mostly wrong – new answer. in your params hash, :filename is not associated with another hash, it is associated with an ActiveDispatch::Http::UploadedFile object. Your last code line: def screenshot_params params.require(:screenshot).permit(:title, assets_attributes: :filename) is actually correct, however, the filename attribute is not being allowed since it is not one of the permitted … Read more