Rails :memory: revisited

As I was creating a new rails app, I used this previous post of mine to set up the in memory database. Well it’s time for an update. Apparently now the rails config/environments/*.rb are run in the context of Rails::Initializer. So instead of creating that context as in the previous post, you simply do:

          alias initialize_database_old initialize_database
          def initialize_database *args
            initialize_database_old
            load "#{RAILS_ROOT}/db/schema.rb" 
          end

02 Dec 22:14 :: 0 comments :: Comment
Tags: programming, ruby, annoyances, rails

Rails and SQLite3: Default Values

Here’s another rails fix. This one is for SQLite3 >= 3.3.8. SQLite decided to return sql strings for the default values shown in table definitions. This broke rails, the rails people got mad and won’t introduce a workaround. Well here’s my workaround.

sqlite_fix.rb:
module ActiveRecord
            module ConnectionAdapters
              class SQLiteAdapter < AbstractAdapter
                def columns table_name, name=nil
                  table_structure(table_name).map do |field|
                    /^'?(.*)'?$/.match field['dflt_value']
                    field['dflt_value'] = $1
                    SQLiteColumn.new(field['name'], field['dflt_value'], field['type'], field['notnull'] == "0")
                  end
                end
              end
            end
          end
Include it at the end of environment.rb. That’s it!

10 Aug 16:30 :: 0 comments :: Comment
Tags: programming, annoyances, troubleshooting, sqlite3, ruby, rails

Rails :memory: testing in SqLite

I wanted to get that nifty :memory: db thing working for the test database in Rails. There are a sparse few articles floating around telling you how to do this. All of them, IMHO, are kinda ugly, or at least not-so-elegant. Here’s my answer, which took a lot of digging around in the inner workings of rails to come by.

First, do what everyone else tells you, put this in your config/database.yml:
          test:
            adapter: sqlite3
            database: ":memory:" 
          
Once you got that, you have to have some way of loading the schema into your memory db every time you run the test. I found that the best place to do this is in config/environments/test.rb. If you have an even better place, please let me know. At first I thought I could just load "#{RAILS_ROOT}/db/schema.rb" and be done with it. Alas, at this point there is no established connection to the db. So we have to modify things in such a way that this cute little command will get run just after establishing the db connection, but before the db is accessed. I did it like so:
          module Rails
            class Initializer
              alias initialize_database_old initialize_database
              def initialize_database
                initialize_database_old
                load "#{RAILS_ROOT}/db/schema.rb" 
              end
            end
          end
          
Then don’t forget to rake db:schema:dump before running rake test.

There’s probably other things you could do, like use ActiveRecord::Base.establish_connection instead of Initializer.initialize_database.

I break the above code block into lib/memdb.rb and simply require that inside of config/environments/test.rb. I tried to just add a metaclass to the config variable you see in test.rb but that is apparently some kind of impostor, because it didn’t work. Anyway, armed with this knowledge someone could probably make a pretty sane plugin to cleanly bring you :memory: testing.

04 Aug 21:58 :: 0 comments :: Comment
Tags: annoyances, ruby, programming, rails

Acts as Dead?

Been a while… So my blog was broken just now, not sure for how long. Not more than a month I think. The problem was in the acts_as_taggable plugin, which rumor has it is dead. For now I will still use it, and describe the fix in case it also is broken for you. The problem is in the join table. The geniuses decided to make the join table <tag class>_<taggable class>. This is backwards from the regular has_and_belongs_to_many convention in activerecord. Somewhere, somehow, this backwardsness was reversed. I had it using it’s default join table before (tags_posts) and it broke as now it’s looking for posts_tags. So I add an option to the call:

acts_as_taggable :join_table => :tags_posts

I suppose I could have changed the table name, but at least this way it will never (hopefully) break again. I’m not quite sure how this default behavior changed in the first place.

04 Jul 13:25 :: 0 comments :: Comment
Tags: annoyances, blog, rails, programming

Rails Ajax Slideshow

I was looking for something else, but stumbled across this. (praise google!) It’s pdf slides for a pretty awesome looking presentation on rails and ajax. Just looking through the slides opened my eyes to some pretty neat stuff I had no clue about. It’s got some funny pictures, too. It would be nice to see the actual presentation that went with the slides.

My favorite lesson from it: I didn’t have a clue about rjs templates and returning javascript for ajax calls. I’m excited to put these to use.

03 May 10:07 :: 0 comments :: Comment
Tags: rails, web, programming, ruby, ajax

Comments

Well, I finally did it. I have comments now. Stop on by and drop one in!

02 May 20:22 :: 3 comments :: Comment
Tags: blog, rails