Convertig a HABTM join table to a real activerecord model

If your has_and_belongs_to_many associations suddenly need to know more, its time to convert them to a real model.

  • rename table
  • add id
  • add created_at/updated_at as not-null (need to set existing records to something)

Code

class CreateJoinModel < ActiveRecord::Migration
  def change
    rename_table :products_users, :purchases
    add_column :purchases, :id, :primary_key
    [:created_at, :updated_at].each do |column|
      add_column :purchases, column, :timestamp, null: false, default: Time.at(0)
      change_column_default :purchases, column, nil
    end
  end
end

Ruby expect alternative, responding to commandline interfaces

A small snippet to interact with commandline applications that ask questions on stdin, particularly useful for testing.

Usage

interact "rails new foo -m template.rb", /RSpec/ => 'y', /MySql/ => 'n'

Code

 # https://gist.github.com/2166521
 # capture commandline output and send responses if nothing was read
  def interact(command, answers)
    puts command
    line = ""
    write = false

    PTY.spawn(command) do |output, input, pid|
      loop do
        rs, ws, = IO.select([output], [input])

        # read into the buffer
        if r = rs[0]
          write = false
          ret = r.read(1)
          print ret

          return unless ret

          line << ret
          line = "" if ret == "\n"
        end

        # time to write ?
        if w = ws[0]
          if write and line != ""
            text = line.gsub(/\e\[\d+m/,'').strip
            answer = answers.detect do |question, answer|
              text =~ question
            end || raise("Question #{text.inspect} has no answer!")
            w.write(answer.last + "\n")
            line = ""
          else
            write = true
          end
        end
      end
    end
    $?.success?
  end