Ruby On Rails
Researching Redis on Rails
by specialj on Jan.08, 2012, under Ruby On Rails
I’ve been researching how good a fit Redis might be for a project or 2. One project needs a memory based key-value store generally without persistence, which would seem to suggest memcached. However, there may be times where persistence across reboots may be desirable and Redis’ atomic operations and data types may also be useful. Here’s some links I’ve been checking out:
- Software
- redis – obviously
- redis-rb – gem for accessing redis from ruby
- redis-objects – gem for mapping Redis types to Ruby objects
- ohm – gem for creating Redis based object models
- supermodel – gem for ActiveModel descendant that can store to Redis
- Information
- Using Redis with Ruby on Rails
- Datastores with ruby: Redis and Ohm (part 1)
- To Redis or Not To Redis? (Key-Value Stores Part 4) | Engine Yard Blog
- Comparing MongoDB and Redis, Part 1 « While I Pondered…
- Comparing MongoDB and Redis, Part 2 « While I Pondered…
- where_redis_is_a_good_fit
- using_keyvalue_stores_from_ruby
how to disable Rack::Cache in Rails 3.1
by specialj on Sep.13, 2011, under Ruby On Rails
Couldn’t find this anywhere but what I found worked was to put this in config/application.rb:
require 'rack/cache'
config.middleware.delete Rack::CacheThis did fix the issue that sent me to this, as many people are likely to run into. The issue was that since Rack::Cache uses the default Rails FileStore that lots and lots of small files were being created in lots and lots of directories. I don’t think Rack::Cache offers me much for this application, but if it did I would look into switching from a file store to a memory or memcached store.
Fixing netbeans in ubuntu
by specialj on May.20, 2011, under Development, Ruby On Rails
Netbeans has some issues in Ubuntu 10.10 and 11.04. One of the most serious is that javascript syntax highlighting does not work. Another is that Rails 3 applications do not run or debug correctly. I found the solution to be going to “Tools – Plugins -Settings” and enabling the “Netbeans” update center. Then by going to “Updates” and clicking “Reload Catalog” I got an update for the update system. After that was installed an netbeans was restarted I got a lot more updates for plugins (15 or so). Once updated everything seemed to work as it should.
More information:
upgrade a rails app from rails 2 to rails 3
by specialj on Apr.24, 2011, under Ruby On Rails
Just a few notes on a recent upgrade with a rails project that was fairly young thus did not have a lot of issues moving to rails 3.
problems upgrading devise in an rails 2 to rails 3 migration
by specialj on Apr.22, 2011, under Ruby On Rails
I ran into some problems upgrading devise from 1.0.x to 1.3.x.
- to use passwords besides bcrypt you have to made 2 changes:
- in config/initializers/devise.rb
- config.encryptor = :sha1 (for devise 1.0.x compatibility)
- in User model (or whatever model calls devise)
- call devise with both :database_authenticatable and :encryptable
- you can tell you have issues with this if you see errors like BCrypt::Errors::InvalidHash or “invalid hash”
- in config/initializers/devise.rb
- browser validation prevents sign in with something other than email
- I create app/views/devise/sessions/new.html.erb from the devise version and changed email_field to text_field
- Can’t log in with “username” (some hidden JS blocking) – Devise | Google Groups
Determining if a string is a float
by specialj on Mar.19, 2011, under Ruby On Rails
Using to_f has the problem of returning 0.0 for invalid strings. So for a project I needed to know if a string was a valid float or not and came across this helpful page: ruby on rails – Determine if a string is a valid float value – Stack Overflow. My favorite option was this:
class String
def valid_float?
# The double negation turns this into an actual boolean true - if you're
# okay with "truthy" values (like 0.0), you can remove it.
!!Float(self) rescue false
end
end
Accessing MS SQL Server from Rails without ODBC
by specialj on Mar.18, 2011, under Ruby On Rails
I found myself looking through the code for activerecord-sqlserver-adapter and noticed a new mode called “dblib”. It turns out this mode can be used when using the tiny_tds gem along with FreeTDS to avoid using ODBC altogether. I decided to try it out on a project and have found it to work very well so far. The update_sql call no longer has to make a call to get the number of affected rows, something which was slowing down the code significantly. I don’t have many benchmarks yet but it looks like performance is up or has remained constant across the board. I’m not sure if there are any problems that might come up, certainly TinyTDS is a relatively new project. But for now I’m enjoying not having to use ODBC and hopefully this will continue to work well moving forward.
Ruby regexp surprise: \w is not equivalent to [0-9A-Za-z_]
by specialj on Mar.17, 2011, under Ruby On Rails
Despite ample documentation to the contrary I was surprised to learn that by default ruby matches \w to all unicode characters. This lead to some strange results in one of my apps where unicode quotes were present and matching \w. I found the solution is to use /\w+/n to specify the n kcode rather than the u kcode. But this was still distressing to find as the default in 1.8.7 and 1.9.2. It was further complicated by trying to get information. The following websites all report that \w is equivalent to [0-9A-Za-z_]:
- Regular expressions (Ruby User’s Guide)
- Rubular: a Ruby regular expression editor and tester
- Ruby Regular Expressions
According to these links, this behavior is spec but not documented anywhere:
Rails marshal data too short
by specialj on Mar.15, 2011, under Ruby On Rails
I ran into an error when trying to save an object in flash between Rails actions. I’m still not sure why marshal data too short happened as the session storage is mysql using the text type which should be 64kb. I can’t imagine the object taking more size than that but it would be worth checking. I resolved the issue by not storing the object but other options would be figuring out why the object is so large (Marshal.dump(object) maybe) or switching to mediumtext type which has a limit of 16MB. To do this via a migration can be found here:
Working with devise for Rails authentication
by specialj on Feb.27, 2011, under Ruby On Rails
I’ve begun using devise for authentication for my various Ruby on Rails applications. It is based on warden which seems a sensible choice. There is also a fairly large community and active development. Devise plays nice with authorization libraries such as CanCan and can be integrated with OmniAuth for oauth/oauth2 authentication. I’ve run into a number of issues so I thought I would track some of the useful links that have helped me out.