Sign in through GitHub

Josh Goodall's Profile

GitHub User: goodallj

Comments by Josh Goodall

Avatar

Whats the advantage of moving your schema into ruby?

I found Hstore invaluable when using STI, because ActiveRecord's column-proliferation approach is an ORM wart of the highest order but I didn't really want to go MongoDB just yet.

Avatar

I concur with Kevin. I've come to Ruby & Rails with a background in other programming languages and frameworks (C, Perl, Scheme and Java). The rails API doco is somewhat half-baked, so I am very interested in deep-dive walkthroughs and I have no problem keeping up. And the pause button is there if one needs to chew on the substance a little more.

Avatar

nice piece, also for the neat metaprogramming example!

NB: playback cuts off a little, missing the last couple of seconds.

Avatar

Nominating this article for an update.

Avatar

Another usage tip. To make creates work (e.g. when using accepts_nested_attributes_for), you'll need serialize :properties, ActiveRecord::Coders::Hstore in your model (assuming your hstore field is called "properties").

I've seen suggestions that this need will go away with Rails 4.0.

Avatar

How about a bit of DRY. I have this in lib/hstore_accessor.rb ( see https://gist.github.com/2834785 ):

ruby
module HstoreAccessor
  def self.included(base)
    base.extend(ClassMethods)
  end
  
  module ClassMethods
    def hstore_accessor(hstore_attribute, *keys)
      Array(keys).flatten.each do |key|
        define_method("#{key}=") do |value|
          send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(key.to_s => value))
          send("#{hstore_attribute}_will_change!")
        end
        define_method(key) do
          send(hstore_attribute) && send(hstore_attribute)[key.to_s]
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, HstoreAccessor)

and in config/initializers/active_record_extensions.rb

ruby
require "hstore_accessor"

Then we can do

ruby
Class Widget < ActiveRecord::Base
  hstore_accessor :properties, :size, :shape, :smell
end