<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <generator>Persumi - Level up your writing and blogging with AI</generator>
  <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
  <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/rails"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/rails/feed/rss"/>
  <link rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/rails/feed/atom"/>
  <author>
    <name>Fred Wu</name>
    <email>ifredwu@gmail.com</email>
    <uri>http://persumi.com/u/fredwu</uri>
  </author>
  <subtitle/>
  <id>http://persumi.com/u/fredwu/tech/e/blog/t/rails</id>
  <title>Blog (rails) - Fred Wu&apos;s Tech</title>
  <updated>2026-04-17T18:08:16.678743Z</updated>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
About a month ago I was in-between jobs - I had two weeks to rest up, recharge and get ready for my new job. So I thought,&amp;nbsp;I should use those two weeks to learn something new.&lt;/p&gt;
&lt;p&gt;
Years ago I briefly looked into &lt;a href=&quot;http://elixir-lang.org/&quot;&gt;Elixir&lt;/a&gt; when it was first released to the wild, at the time I wasn’t interested in picking it up due to its syntax similarity to Ruby, despite their vastly different underlying semantics. I love Ruby, and it’s been my weapon of choice for the past 6-7 years, so when it came time for me to learn something &lt;em&gt;new&lt;/em&gt;, I naturally wanted to learn something a bit more different than Ruby, syntax-wise.&lt;/p&gt;
&lt;p&gt;
Fast-forward a few years, I am more mature and open-minded, and are now in a position to welcome Elixir and to embrace the Ruby-like syntax as well as the functional programming mindset with open arms.&lt;/p&gt;
&lt;h2&gt;
Learning Elixir&lt;/h2&gt;
&lt;p&gt;
Given the strong influence of Ruby in Elixir, I can’t help but get nostalgic about learning it by reading another great book by Dave Thomas: &lt;a href=&quot;https://pragprog.com/book/elixir12/programming-elixir-1-2&quot;&gt;Programming Elixir&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
Dave Thomas’ original &lt;a href=&quot;https://pragprog.com/book/ruby/programming-ruby&quot;&gt;Programming Ruby&lt;/a&gt; was instrumental in the success of the Ruby programming language and its communities in the west, and it certainly has helped me to greatly widen my exposure to not only the wonderful world of Ruby but object-oriented programming in general as a PHP developer.&lt;/p&gt;
&lt;p&gt;
Learning Elixir has been really fun, not only does it share the same developer-friendliness championed by Ruby, it also does so with remarkable strength in concurrency thanks to BEAM (Erlang’s VM).&lt;/p&gt;
&lt;p&gt;
Many people are drawn to Elixir due to its Ruby influence, its functional and immutability nature, and its &lt;a href=&quot;https://en.wikipedia.org/wiki/Actor_model&quot;&gt;Actor-based concurrency model&lt;/a&gt;. I would however like to call out one of my favourite features of Elixir that sometimes gets overlooked, and that is how you could write unit tests using &lt;a href=&quot;http://elixir-lang.org/docs/stable/ex_unit/ExUnit.DocTest.html&quot;&gt;&lt;code class=&quot;inline&quot;&gt;ExUnit.DocTest&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
Elixir Makes Writing Unit Tests Effortless&lt;/h3&gt;
&lt;p&gt;
Python programmers have been writing doctests for years, but Ruby due to not having an equivalent standard library, has never had writing doctests taken off in its community. I’m glad Elixir has.&lt;/p&gt;
&lt;p&gt;
Take a look at the code snippet below:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;elixir language-elixir&quot;&gt;defmodule Stemmer.Step0 do
  @doc &quot;&quot;&quot;
  ## Examples

      iex&gt; Stemmer.Step0.trim_apostrophes(&quot;&apos;ok&quot;)
      &quot;ok&quot;

      iex&gt; Stemmer.Step0.trim_apostrophes(&quot;o&apos;k&quot;)
      &quot;o&apos;k&quot;

      iex&gt; Stemmer.Step0.trim_apostrophes(&quot;&apos;o&apos;k&apos;&quot;)
      &quot;o&apos;k&quot;
  &quot;&quot;&quot;
  def trim_apostrophes(word) do
    word
    |&gt; String.replace_prefix(&quot;&apos;&quot;, &quot;&quot;)
    |&gt; String.replace_suffix(&quot;&apos;&quot;, &quot;&quot;)
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
The three test cases given will actually be tested by ExUnit, provided you ask it to do so as below:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;elixir language-elixir&quot;&gt;defmodule Stemmer.Step0Test do
  use ExUnit.Case, async: true

  doctest Stemmer.Step0
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
You may think having doctests is not a big deal, what I can say is that I am really enjoying the fact that &lt;strong&gt;unit tests can be written with minimal friction and high visibility&lt;/strong&gt; (as they sit with the implementation, rather than within a big test suite with a sea of files). Besides, not all of us practice TDD religiously or 100% of the time, and when I don’t or can’t do TDD, having doctests ensures I don’t forget adding test cases. :)&lt;/p&gt;
&lt;h3&gt;
Toy Robot in Elixir&lt;/h3&gt;
&lt;p&gt;
The best way to learn something new is to practice it as you learn it. Instead of diving straight into a complicated system, or to write a &lt;code class=&quot;inline&quot;&gt;hello world&lt;/code&gt; program, I thought I would dig out a code test and re-implement it in Elixir.&lt;/p&gt;
&lt;p&gt;
The code test is the rather infamous &lt;a href=&quot;https://github.com/search?q=toy+robot&quot;&gt;Toy Robot&lt;/a&gt; test. As an interviewer, I must have reviewed a few hundreds of these tests, most of which in Ruby. I know what a good OO solution looks like, so naturally I was looking forward to “rethink” the problem and have a crack at it using Elixir.&lt;/p&gt;
&lt;p&gt;
Here is the result: &lt;a href=&quot;https://github.com/fredwu/toy-robot-elixir&quot;&gt;https://github.com/fredwu/toy-robot-elixir&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
As a learner, I wanted to practice as many language features as possible, so included in the Toy Robot test code I tried:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
Different data structures such as Struct and List  &lt;/li&gt;
  &lt;li&gt;
Pattern matching  &lt;/li&gt;
  &lt;li&gt;
Data piping (&lt;code class=&quot;inline&quot;&gt;|&gt;&lt;/code&gt;)  &lt;/li&gt;
  &lt;li&gt;
&lt;code class=&quot;inline&quot;&gt;Agent&lt;/code&gt; for managing states  &lt;/li&gt;
  &lt;li&gt;
Macros for validation rules  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
I kept the test code small and nimble on purpose to illustrate the readability of a highly expressive language. I may not have exercised all the language features (such as supervisors and protocols), but at that point I felt I could start the Elixir journey with a big smile on my face already.&lt;/p&gt;
&lt;h2&gt;
Learning Phoenix&lt;/h2&gt;
&lt;p&gt;
I have to admit, a big part of the reason that got me started on learning Elixir is to build a side project. Initially I wanted to simply use Ruby and Rails because I can be very productive using them, but in the end I decided on learning Elixir and &lt;a href=&quot;http://www.phoenixframework.org/&quot;&gt;Phoenix&lt;/a&gt;, because &lt;strong&gt;most side projects fail, and when they do, how much learning can you extract out of those experiences?&lt;/strong&gt; My answer to this question, is to learn a new language, a new framework and most importantly a new programming paradigm to drastically increase the amount of learning I could gain.&lt;/p&gt;
&lt;p&gt;
But please, before you jump on Phoenix, learn Elixir properly first! Over the years I’ve seen far too many cases of people jumping on Rails without understanding the language features of Ruby…&lt;/p&gt;
&lt;p&gt;
To learn Phoenix, the &lt;a href=&quot;https://pragprog.com/book/phoenix/programming-phoenix&quot;&gt;Programming Phoenix&lt;/a&gt; book is pretty much the bible on this subject aside from the official guide, and it is written by Chris McCord, the author of Phoenix, Bruce Tate, and Jose Valim, the author of Elixir.&lt;/p&gt;
&lt;p&gt;
As an experienced Rails developer, it only took me a day or two to go through the book (I largely skimmed the chapters on &lt;code class=&quot;inline&quot;&gt;Channels&lt;/code&gt; as I don’t intend to build a real-time app).&lt;/p&gt;
&lt;p&gt;
Now, as a Ruby developer, the biggest pain point when working on Rails or other sizeable Ruby MVC projects for me, is ActiveRecord. I got so frustrated to the status quo I even &lt;a href=&quot;https://github.com/fredwu/datamappify&quot;&gt;attempted to fix it&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
Fortunately, in Elixir and Phoenix we have &lt;a href=&quot;https://github.com/elixir-ecto/ecto&quot;&gt;Ecto&lt;/a&gt;. One of my favourite features of Ecto is the concept of &lt;a href=&quot;https://hexdocs.pm/ecto/Ecto.Changeset.html&quot;&gt;Changeset&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
Take a look at the code snippet below:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;elixir language-elixir&quot;&gt;defmodule MySecretApp.User do
  use MySecretApp.Web, :model

  schema &quot;users&quot; do
    field :username, :string
    field :email, :string
    field :password, :string, virtual: true
    field :encrypted_password, :string

    has_one :profile, MySecretApp.Profile

    timestamps()
  end

  def changeset(struct, params \\ %{}) do
    struct
    |&gt; cast(params, [:username, :email])
    |&gt; validate_required([:username, :email])
    |&gt; validate_length(:username, min: 3, max: 20)
    |&gt; validate_format(:username, ~r/\A[a-zA-Z0-9_]+\z/, message: &quot;alphanumeric and underscores only&quot;)
    |&gt; validate_format(:email, ~r/@/)
    |&gt; unique_constraint(:username)
    |&gt; unique_constraint(:email)
  end

  def creation_changeset(struct, params \\ %{}) do
    struct
    |&gt; changeset(params)
    |&gt; password_changeset(params)
  end


  defp password_changeset(struct, params) do
    struct
    |&gt; cast(params, [:password])
    |&gt; validate_required([:password])
    |&gt; validate_length(:password, min: 8, max: 200)
    |&gt; encrypt_password
  end

  defp encrypt_password(changeset) do
    case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: password}} -&gt;
        put_change(changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(password))
      _ -&gt;
        changeset
    end
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
The &lt;code class=&quot;inline&quot;&gt;changset/2&lt;/code&gt; function can be used when a user needs to be updated, whereas the &lt;code class=&quot;inline&quot;&gt;creation_changeset/2&lt;/code&gt; is to be used only when a user is first created. Sure, you can achieve similar result in Rails by using custom validators, but the fact that this practice is enforced by the library and the framework, is encouraging.&lt;/p&gt;
&lt;p&gt;
One other thing that I’ve seen in larger Rails apps, is the leaky abstraction of view-level logic, they typically sit in controllers, helpers (which are globally available) or worse, models. Phoenix in this case follows what &lt;a href=&quot;http://hanamirb.org/&quot;&gt;Hanami&lt;/a&gt;, &lt;a href=&quot;http://trailblazer.to/&quot;&gt;Trailblazer&lt;/a&gt; and many other frameworks do: it introduces a “view model” layer.&lt;/p&gt;
&lt;p&gt;
Something along the lines of:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;elixir language-elixir&quot;&gt;defmodule MySecretApp.UserView do
  use MySecretApp.Web, :view

  def full_name do
    &quot;#{title} #{first_name} #{last_name}&quot;
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
In a nutshell, Phoenix is just like Rails, but less magical (in a good way) and faster. :) Like the Elixir eco-system, most libraries and concepts feel like their ruby/rails counterparts, but more refined.&lt;/p&gt;
&lt;p&gt;
There are a few popular &lt;a href=&quot;https://hex.pm/&quot;&gt;Hex&lt;/a&gt; packages if you’re familiar with their ruby counterparts:&lt;/p&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: left;&quot;&gt;
      &lt;/th&gt;
      &lt;th style=&quot;text-align: left;&quot;&gt;
Elixir      &lt;/th&gt;
      &lt;th style=&quot;text-align: left;&quot;&gt;
Ruby      &lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
Code analysis      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://hex.pm/packages/credo&quot;&gt;credo&lt;/a&gt;      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://rubygems.org/gems/rubocop&quot;&gt;rubocop&lt;/a&gt;      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
Testing      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://hex.pm/packages/espec&quot;&gt;espec&lt;/a&gt;      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://rubygems.org/gems/rspec&quot;&gt;rspec&lt;/a&gt;      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
Browser testing      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://hex.pm/packages/wallaby&quot;&gt;wallaby&lt;/a&gt;      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://rubygems.org/gems/capybara&quot;&gt;capybara&lt;/a&gt;      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
Test coverage      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://hex.pm/packages/excoveralls&quot;&gt;excoveralls&lt;/a&gt;      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://rubygems.org/gems/simplecov&quot;&gt;simplecov&lt;/a&gt;      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
Test factory      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://hex.pm/packages/ex_machina&quot;&gt;ex_machina&lt;/a&gt;      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://rubygems.org/gems/factory_girl&quot;&gt;factory_girl&lt;/a&gt;      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
Authentication      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://hex.pm/packages/guardian&quot;&gt;guardian&lt;/a&gt;      &lt;/td&gt;
      &lt;td style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://rubygems.org/gems/devise&quot;&gt;devise&lt;/a&gt;      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
And the list goes on and on… Be sure to check out &lt;a href=&quot;https://github.com/h4cc/awesome-elixir/&quot;&gt;Awesome Elixir&lt;/a&gt; for more community curated Elixir libraries.&lt;/p&gt;
&lt;h2&gt;
Learning Machine Learning&lt;/h2&gt;
&lt;p&gt;
As I started building the foundation of my side project, you know, the usual user management and session management, etc, etc, by chance I came across mentions of &lt;a href=&quot;https://en.wikipedia.org/wiki/Bayesian_inference&quot;&gt;Bayesian inference&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
One thing led to another, very soon I started looking into varies different machine learning algorithms such as &lt;a href=&quot;https://en.wikipedia.org/wiki/Naive_Bayes_classifier&quot;&gt;Naive Bayes&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Random_forest&quot;&gt;Random Forest&lt;/a&gt;. These are all subjects I had never come across or even heard of before, as I have no strong mathematics, statistics or computer science background. I was however, intrigued and inspired nonetheless, and wanted to employ some machine learning in my side project.&lt;/p&gt;
&lt;p&gt;
And the best way to understand and learn about machine learning algorithms? You guessed, is to write one! After some research, I came to the conclusion that Naive Bayes is one of the simplest to implement, is great for text classification which is useful for my side project, and has good accuracy given its simplicity and fast speed.&lt;/p&gt;
&lt;h3&gt;
Introducing &lt;a href=&quot;https://github.com/fredwu/simple_bayes&quot;&gt;Simple Bayes&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;
So, after a few train rides to and from work, on my newly purchased Macbook (yes, the one with only a single USB-C port), I built a Naive Bayes library: &lt;a href=&quot;https://github.com/fredwu/simple_bayes&quot;&gt;Simple Bayes&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
As of writing, this is the only Naive Bayes library in Elixir that supports the following features:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
Naive Bayes algorithm with different models    &lt;ul&gt;
      &lt;li&gt;
Multinomial      &lt;/li&gt;
      &lt;li&gt;
Binarized (boolean) multinomial      &lt;/li&gt;
      &lt;li&gt;
Bernoulli      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
No external dependencies  &lt;/li&gt;
  &lt;li&gt;
Ignores stop words  &lt;/li&gt;
  &lt;li&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/Additive_smoothing&quot;&gt;Additive smoothing&lt;/a&gt;  &lt;/li&gt;
  &lt;li&gt;
&lt;a href=&quot;https://en.wikipedia.org/wiki/Tf-idf&quot;&gt;TF-IDF&lt;/a&gt;  &lt;/li&gt;
  &lt;li&gt;
Optional keywords weighting  &lt;/li&gt;
  &lt;li&gt;
Optional word stemming via &lt;a href=&quot;https://github.com/fredwu/stemmer&quot;&gt;Stemmer&lt;/a&gt;  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Let’s see it in action shall we? :)&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;elixir language-elixir&quot;&gt;SimpleBayes.init
|&gt; SimpleBayes.train(:ruby, &quot;I enjoyed using Rails and ActiveRecord for the most part.&quot;)
|&gt; SimpleBayes.train(:ruby, &quot;The Ruby community is awesome.&quot;)
|&gt; SimpleBayes.train(:ruby, &quot;There is a new framework called Hanami that&apos;s promising.&quot;)
|&gt; SimpleBayes.train(:ruby, &quot;Please learn Ruby before you learn Rails.&quot;)
|&gt; SimpleBayes.train(:ruby, &quot;We use Rails at work.&quot;)
|&gt; SimpleBayes.train(:elixir, &quot;It has Phoenix which is a Rails-like framework.&quot;)
|&gt; SimpleBayes.train(:elixir, &quot;Its author is a Rails core member, Jose Valim.&quot;)
|&gt; SimpleBayes.train(:elixir, &quot;Phoenix and Rails are on many levels, comparable.&quot;)
|&gt; SimpleBayes.train(:elixir, &quot;Phoenix has great performance.&quot;)
|&gt; SimpleBayes.train(:elixir, &quot;I love Elixir.&quot;)
|&gt; SimpleBayes.train(:php, &quot;I haven&apos;t written any PHP in years.&quot;)
|&gt; SimpleBayes.train(:php, &quot;The PHP framework Laravel is inspired by Rails.&quot;)
|&gt; SimpleBayes.classify(&quot;I wrote some Rails code at work today.&quot;)
# =&gt; [
# ruby: 0.20761437345986136,
# elixir: 0.08101868169313056,
# php: 0.019047884912605735
# ]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see, provided with reasonable training data, Naive Bayes can work extremely well.&lt;/p&gt;
&lt;h3&gt;
Introducing &lt;a href=&quot;https://github.com/fredwu/stemmer&quot;&gt;Stemmer&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;
Something I discovered as I was building Simple Bayes, is something called &lt;a href=&quot;https://en.wikipedia.org/wiki/Stemming&quot;&gt;stemming&lt;/a&gt;. Let’s see another example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;elixir language-elixir&quot;&gt;SimpleBayes.init(stem: false)
|&gt; SimpleBayes.train(:apple, &quot;buying apple&quot;)
|&gt; SimpleBayes.train(:banana, &quot;buy banana&quot;)
|&gt; SimpleBayes.classify(&quot;buy apple&quot;)
# =&gt; [
# banana: 0.057143654737817205,
# apple: 0.057143654737817205
# ]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Oops, the probabilities of the sentence “buy apple” of being &lt;code class=&quot;inline&quot;&gt;apple&lt;/code&gt; and &lt;code class=&quot;inline&quot;&gt;banana&lt;/code&gt; are the same, that’s not good, as we know “buying” and “buy” should mean the same thing. This is where stemming comes in handy. Let’s enable stemming and run the example again:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;elixir language-elixir&quot;&gt;SimpleBayes.init(stem: true)
|&gt; SimpleBayes.train(:apple, &quot;buying apple&quot;)
|&gt; SimpleBayes.train(:banana, &quot;buy banana&quot;)
|&gt; SimpleBayes.classify(&quot;buy apple&quot;)
# =&gt; [
# apple: 0.18096114003107086,
# banana: 0.1505149978319906
# ]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Stemming in this case correctly identified “buy” and “buying” as the same thing (they both have the stemmed root of “buy”).&lt;/p&gt;
&lt;p&gt;
How did I include stemming in Simple Bayes you wonder? Let me introduce you to &lt;a href=&quot;https://github.com/fredwu/stemmer&quot;&gt;Stemmer&lt;/a&gt; - as of writing this is the only Porter2 stemmer available in Elixir. :)&lt;/p&gt;
&lt;p&gt;
Elixir and BEAM may not be known for their raw performance, but compared to a similar Porter2 stemmer implemented in pure ruby, my Elixir version runs about five times faster (under &lt;code class=&quot;inline&quot;&gt;5&lt;/code&gt;s to stem 29000+ words, compared to &lt;code class=&quot;inline&quot;&gt;25&lt;/code&gt;s with the ruby version).&lt;/p&gt;
&lt;h2&gt;
The Learning Continues…&lt;/h2&gt;
&lt;p&gt;
I’ve only been writing Elixir for a month, and there are still heaps to learn and to practice. So far though, I have to say my experience with both Elixir and Phoenix has been fantastic - I get the same satisfying and pumped feeling I got when I was learning Ruby and Rails years ago.&lt;/p&gt;
&lt;p&gt;
If you haven’t checked out Elixir, I strongly encourage you to do so, after all, the world seems to be moving in the concurrency direction at an increasingly rapid speed, including &lt;a href=&quot;http://engineering.appfolio.com/appfolio-engineering/2015/11/18/ruby-3x3&quot;&gt;Ruby 3x3&lt;/a&gt;. :)&lt;/p&gt;
]]&gt;</content>
    <published>2016-07-23T18:33:29.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/i-accidentally-some-machine-learning-my-story-of-a-month-of-learning-elixir"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/i-accidentally-some-machine-learning-my-story-of-a-month-of-learning-elixir</id>
    <title>I Accidentally Some Machine Learning - My Story of A Month of Learning Elixir</title>
    <updated>2016-07-23T18:33:29.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
&lt;em&gt;This post is about the ruby library we are building - &lt;a href=&quot;https://github.com/fredwu/datamappify&quot;&gt;Datamappify&lt;/a&gt;, please go check it out.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
At &lt;a href=&quot;http://www.locomote.com.au/&quot;&gt;Locomote&lt;/a&gt; we are building a relatively large web application using Rails. Before we began to lay the foundation, we knew very well that if we wanted the project to be maintainable we had to architect the system with extra care and attention. More specifically, we can’t rely on simply using ActiveRecord which combines behaviour and persistence as our domain models.&lt;/p&gt;
&lt;p&gt;
We began our search for something that would help us decouple our application from the domain layer down to the form handling. We’ve found a couple of gems that are close to what we were after - &lt;a href=&quot;https://github.com/braintree/curator&quot;&gt;Curator&lt;/a&gt;, &lt;a href=&quot;https://github.com/joakimk/minimapper&quot;&gt;Minimapper&lt;/a&gt;, &lt;a href=&quot;https://github.com/nulogy/edr&quot;&gt;Edr&lt;/a&gt; and later on &lt;a href=&quot;https://github.com/apotonick/reform&quot;&gt;Reform&lt;/a&gt;. They are all wonderful gems but unfortunately none of them has everything we need.&lt;/p&gt;
&lt;p&gt;
Here are the things we need:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
Decouple domain logic and data persistence.  &lt;/li&gt;
  &lt;li&gt;
Decouple models and forms.  &lt;/li&gt;
  &lt;li&gt;
Support ActiveRecord (or at the very least, ActiveModel) so we can still use many of the awesome gems like &lt;a href=&quot;https://github.com/plataformatec/devise&quot;&gt;Devise&lt;/a&gt;, &lt;a href=&quot;https://github.com/plataformatec/simple_form&quot;&gt;SimpleForm&lt;/a&gt; and &lt;a href=&quot;https://github.com/carrierwaveuploader/carrierwave&quot;&gt;CarrierWave&lt;/a&gt;.  &lt;/li&gt;
  &lt;li&gt;
Support attributes mapped from different data sources (e.g. remote web services from third-party vendors).  &lt;/li&gt;
  &lt;li&gt;
Support lazy loading so that attributes stored on remote data sources will not get triggered upon loading the entities.  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
All things considered, we bit the bullet and started working on &lt;a href=&quot;https://github.com/fredwu/datamappify&quot;&gt;Datamappify&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
Before I go into details and examples, here is an extremely simplified overview of Datamappify’s architecture:&lt;/p&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/I9GpLds.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;
As you can see, Datamappify is loosely based on &lt;a href=&quot;http://martinfowler.com/eaaCatalog/repository.html&quot;&gt;Repository pattern&lt;/a&gt; and &lt;a href=&quot;http://msdn.microsoft.com/en-au/library/ff649505.aspx&quot;&gt;Entity Aggregation pattern&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
Datamappify has three main design goals:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;
To utilise the powerfulness of existing ORMs so that using Datamappify doesn’t interrupt too much of your current workflow. For example, Devise would still work if you use it with a UserAccount ActiveRecord model that is attached to a User entity managed by Datamappify.  &lt;/li&gt;
  &lt;li&gt;
To have a flexible entity model that works great with dealing with form data. For example, SimpleForm would still work with nested attributes from different ORM models if you map entity attributes smartly in your repositories managed by Datamappify.  &lt;/li&gt;
  &lt;li&gt;
To have a set of data providers to encapsulate the handling of how the data is persisted. This is especially useful for dealing with external data sources such as a web service. For example, by calling UserRepository.save(user), certain attributes of the user entity are now persisted on a remote web service. Better yet, dirty tracking and lazy loading are supported out of the box!  &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
You can read more about Datamappify in the project’s &lt;a href=&quot;https://github.com/fredwu/datamappify/blob/master/README.md&quot;&gt;README&lt;/a&gt;. For this blog post I will focus on how we use Datamappify in our Rails application. We are still early days in our application development and Datamappify still has quirks and issues, but I am hoping this post will illustrate some of the key benefits of Datamappify.&lt;/p&gt;
&lt;h3&gt;
Getting Started&lt;/h3&gt;
&lt;p&gt;
Getting started with using Datamappify is really easy. We simply include it in the &lt;code class=&quot;inline&quot;&gt;Gemfile&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;gem &apos;datamappify&apos;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
To keep things organised, we put entities and repositories in their respective directories under &lt;code class=&quot;inline&quot;&gt;app&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_inline_mp1vigkh0f1qz4rgp.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
&lt;h3&gt;
Entities&lt;/h3&gt;
&lt;p&gt;
The reason we wanted Datamappify to utilise existing ORMs like ActiveRecord, is so that we could still use gems like Devise.&lt;/p&gt;
&lt;p&gt;
So, we have a &lt;code class=&quot;inline&quot;&gt;UserAccount&lt;/code&gt; model that handles authentication:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;class UserAccount &lt; ActiveRecord::Base
  devise :database_authenticatable,
          :recoverable, :rememberable, :trackable, :validatable
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;code class=&quot;inline&quot;&gt;UserAccount&lt;/code&gt; has one and only one purpose - user account authentication. Other user behaviours would be contained in either the &lt;code class=&quot;inline&quot;&gt;User&lt;/code&gt; entity itself or service objects. Speaking of the &lt;code class=&quot;inline&quot;&gt;User&lt;/code&gt; entity, it looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;class User
  include Datamappify::Entity

  attr_accessor :user_account

  attribute :account_username, String
  attribute :account_email, String
  attribute :first_name, String
  attribute :last_name, String
  attribute :activated, Boolean, :default =&gt; true

  attributes_from Contact, prefix_with: :work

  validates :first_name, presence: true
  validates :last_name, presence: true

  references :agency

  def full_name
    &quot;#{first_name} #{last_name}&quot;
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
We include &lt;code class=&quot;inline&quot;&gt;Datamappify::Entity&lt;/code&gt; in the &lt;code class=&quot;inline&quot;&gt;User&lt;/code&gt; class to make it an entity. We set the &lt;code class=&quot;inline&quot;&gt;:user_account&lt;/code&gt; accessor is so that we could attach the &lt;code class=&quot;inline&quot;&gt;UserAccount&lt;/code&gt; object onto the entity.&lt;/p&gt;
&lt;p&gt;
The &lt;code class=&quot;inline&quot;&gt;attribute&lt;/code&gt; DSL is from &lt;a href=&quot;https://github.com/solnic/virtus&quot;&gt;Virtus&lt;/a&gt; - we get attribute type coercion for free, awesome!&lt;/p&gt;
&lt;p&gt;
&lt;code class=&quot;inline&quot;&gt;attributes_from&lt;/code&gt; is a DSL provided by Datamappify - it essentially “mounts” all the attributes from another entity, in this case the &lt;code class=&quot;inline&quot;&gt;Contact&lt;/code&gt; entity, which looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;class Contact
  include Datamappify::Entity

  attribute :email, String
  attribute :phone_number, String
  attribute :fax_number, String

  validates :phone_number, presence: true
  validates :fax_number, presence: true
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
All the attributes and validation rules from &lt;code class=&quot;inline&quot;&gt;Contact&lt;/code&gt; are now “mounted” on &lt;code class=&quot;inline&quot;&gt;User&lt;/code&gt;. &lt;code class=&quot;inline&quot;&gt;attributes_from Contact, prefix_with: :work&lt;/code&gt; is equivalent to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;attribute :work_email, String
attribute :work_phone_number, String
attribute :work_fax_number, String

validates :work_phone_number, presence: true
validates :work_fax_number, presence: true&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Validation DSL is provided by &lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveModel/Validations.html&quot;&gt;ActiveModel::Validations&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
&lt;code class=&quot;inline&quot;&gt;references :agency&lt;/code&gt; is a convenient DSL provided by Datamappify. It is equivalent to:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;attr_accessor :agency

attribute :agency_id, Integer&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Now that we have entities, we need a way to retrieve and store them. For that we need repositories.&lt;/p&gt;
&lt;h3&gt;
Repositories&lt;/h3&gt;
&lt;p&gt;
Here is the user repository:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;class UserRepository
  include Datamappify::Repository

  for_entity User

  default_provider :ActiveRecord

  map_attribute :account_username, &apos;ActiveRecord::UserAccount#username&apos;
  map_attribute :account_email, &apos;ActiveRecord::UserAccount#email&apos;

  map_attribute :work_email, &apos;ActiveRecord::Contact#email&apos;
  map_attribute :work_phone_number, &apos;ActiveRecord::Contact#phone_number&apos;
  map_attribute :work_fax_number, &apos;ActiveRecord::Contact#fax_number&apos;
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Similarly to an entity, we include &lt;code class=&quot;inline&quot;&gt;Datamappify::Repository&lt;/code&gt; to make the class a repository. We specify &lt;code class=&quot;inline&quot;&gt;for_entity&lt;/code&gt; to link the repository to an entity, and &lt;code class=&quot;inline&quot;&gt;default_provider&lt;/code&gt; to use a specific data provider for unmapped attributes.&lt;/p&gt;
&lt;p&gt;
Unmapped attributes are the ones not specified in &lt;code class=&quot;inline&quot;&gt;map_attribute&lt;/code&gt;, in this case they are &lt;code class=&quot;inline&quot;&gt;first_name&lt;/code&gt;, &lt;code class=&quot;inline&quot;&gt;last_name&lt;/code&gt; and &lt;code class=&quot;inline&quot;&gt;activated&lt;/code&gt;. Unmapped attributes are actually automatically mapped by Datamappify, so the user repository essentially does this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;map_attribute :first_name, &apos;ActiveRecord::User#first_name&apos;
map_attribute :last_name, &apos;ActiveRecord::User#last_name&apos;
map_attribute :activated, &apos;ActiveRecord::User#activated&apos;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
The first argument of &lt;code class=&quot;inline&quot;&gt;map_attribute&lt;/code&gt; is the name of the attribute from the &lt;code class=&quot;inline&quot;&gt;User&lt;/code&gt; entity (e.g. &lt;code class=&quot;inline&quot;&gt;:first_name&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;
The second argument is a string containing three things:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;
&lt;code class=&quot;inline&quot;&gt;ActiveRecord&lt;/code&gt; is the data provider.  &lt;/li&gt;
  &lt;li&gt;
&lt;code class=&quot;inline&quot;&gt;::User&lt;/code&gt; is the ActiveRecord model class.  &lt;/li&gt;
  &lt;li&gt;
&lt;code class=&quot;inline&quot;&gt;#first_name&lt;/code&gt; is the ActiveRecord attribute from the model class.  &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Even though the &lt;code class=&quot;inline&quot;&gt;User&lt;/code&gt; entity is a representation of a user on the domain level, the underlying data structure does not necessary have to be. As you can see from the user repository example, we are mapping &lt;code class=&quot;inline&quot;&gt;:account_username&lt;/code&gt; and &lt;code class=&quot;inline&quot;&gt;:account_email&lt;/code&gt; to the &lt;code class=&quot;inline&quot;&gt;UserAccount&lt;/code&gt; ActiveRecord model we’ve seen before. And we have a bunch of contact details attributes mapped to the &lt;code class=&quot;inline&quot;&gt;Contact&lt;/code&gt; ActiveRecord model.&lt;/p&gt;
&lt;p&gt;
The database schema therefore looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;create_table &quot;contacts&quot;, force: true do |t|
  t.string &quot;email&quot;
  t.string &quot;phone_number&quot;
  t.string &quot;fax_number&quot;
  t.integer &quot;user_id&quot;
end

create_table &quot;user_accounts&quot;, force: true do |t|
  t.string &quot;username&quot;, default: &quot;&quot;, null: false
  t.string &quot;email&quot;, default: &quot;&quot;, null: false
  t.string &quot;encrypted_password&quot;, default: &quot;&quot;, null: false
  t.string &quot;reset_password_token&quot;
  t.datetime &quot;reset_password_sent_at&quot;
  t.datetime &quot;remember_created_at&quot;
  t.integer &quot;sign_in_count&quot;, default: 0
  t.datetime &quot;current_sign_in_at&quot;
  t.datetime &quot;last_sign_in_at&quot;
  t.string &quot;current_sign_in_ip&quot;
  t.string &quot;last_sign_in_ip&quot;
  t.integer &quot;user_id&quot;
  t.datetime &quot;created_at&quot;
  t.datetime &quot;updated_at&quot;
end

create_table &quot;users&quot;, force: true do |t|
  t.string &quot;first_name&quot;
  t.string &quot;last_name&quot;
  t.boolean &quot;activated&quot;
  t.integer &quot;agency_id&quot;
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Note that because the &lt;code class=&quot;inline&quot;&gt;Contact&lt;/code&gt; entity is mounted on the &lt;code class=&quot;inline&quot;&gt;User&lt;/code&gt; entity, we need a foreign key &lt;code class=&quot;inline&quot;&gt;user_id&lt;/code&gt; in the &lt;code class=&quot;inline&quot;&gt;contacts&lt;/code&gt; table to link them.&lt;/p&gt;
&lt;h4&gt;
Data providers&lt;/h4&gt;
&lt;p&gt;
Because we are allowed to specify a data provider (i.e. &lt;code class=&quot;inline&quot;&gt;ActiveRecord&lt;/code&gt;) for each attribute, we can map attributes to entirely different data providers! For instance, we could have:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;map_attribute :first_name, &apos;ActiveRecord::User#first_name&apos;
map_attribute :last_name, &apos;ActiveRecord::User#last_name&apos;
map_attribute :activated, &apos;Sequel::UserActivation#activated&apos;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Now the &lt;code class=&quot;inline&quot;&gt;activated&lt;/code&gt; attribute is mapped to the &lt;code class=&quot;inline&quot;&gt;UserActivation&lt;/code&gt; Sequel model. This powerful feature would allow us to develop data providers that communicate with remote web services. :)&lt;/p&gt;
&lt;h4&gt;
Repository APIs&lt;/h4&gt;
&lt;p&gt;
Datamappify provides a bunch of APIs for retrieving and storing data. These APIs are being developed as needed during our application development.&lt;/p&gt;
&lt;p&gt;
For instance, to find a particular user entity by ID:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;user = UserRepository.find(1)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
To get all the users:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;users = UserRepository.all&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
To search for users:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;users = UserRepository.find(:first_name =&gt; &apos;Fred&apos;, :activated =&gt; true)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
To save a user:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;UserRepository.save(user)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
To delete a user:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;UserRepository.destroy(user)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
There are some limitations for certain APIs, you can &lt;a href=&quot;https://github.com/fredwu/datamappify/blob/master/README.md#repository-apis&quot;&gt;read more about them here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
When saving an entity, because Datamappify has an internal pool for tracking dirty attributes, only those dirty attributes will get saved.&lt;/p&gt;
&lt;p&gt;
Also, Datamappify supports attribute lazy loading, all we have to do is to tell our entity to become lazy aware:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;class User
  include Datamappify::Entity
  include Datamappify::Lazy
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Once an entity is lazy aware, a repository will inject the lazy loading mechanism onto the entity when it retrieves such entity (via &lt;code class=&quot;inline&quot;&gt;find&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;
Both repositories and entities support inheritance, again, you may read more about them in the &lt;a href=&quot;https://github.com/fredwu/datamappify/blob/master/README.md&quot;&gt;project’s README&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
Putting Things Together With A Controller&lt;/h3&gt;
&lt;p&gt;
Remember we have the &lt;code class=&quot;inline&quot;&gt;UserAccount&lt;/code&gt; for authentication? So how does that work? Well, here’s the piece of code in our application controller:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;class ApplicationController &lt; ActionController::Base
  before_filter :authenticate_user_account!

  private

  def current_user
    unless current_user_account.blank?
      @current_user ||= UserRepository.find(current_user_account.user_id)
    end
  end

  helper_method :current_user
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Not too different from the normal Devise workflow hey? ;)&lt;/p&gt;
&lt;h3&gt;
Forms&lt;/h3&gt;
&lt;p&gt;
Web applications typically have lots of forms - ours is no different. It turns out, Datamappify can help build form objects too!&lt;/p&gt;
&lt;p&gt;
Here’s the view portion of one of our forms (we use &lt;a href=&quot;https://github.com/slim-template/slim&quot;&gt;Slim&lt;/a&gt; templates):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;# our standard form layout

= simple_form_for submission_target, url: submission_path, signed: true do |f|

  = yield(f)

  .form-actions
    .pull-left
      - if archivable? &amp;&amp; resource.persisted?
        = link_to &apos;Archive&apos;, &apos;#&apos;, method: :delete, class: &apos;btn-archive&apos;

    = f.submit &apos;Save&apos;
    = link_to &apos;Cancel&apos;, cancel_path

# actual form content

= render layout: &apos;forms/resource&apos; do |f|
  h3 Account Details
  .row-fluid
    .span4 = f.input :account_type, collection: BankAccount::ACCOUNT_TYPES.invert
    .span4 = f.input :account_code
    .span4 = f.input :account_name
  .row-fluid
    .span4 = f.input :account_number
    .span4 = f.input :bank_name
    .span4 = f.input :bank_branch
  .row-fluid
    .span4 = f.input :bank_bsb, label: &quot;Bank BSB&quot;
    .span4 = f.input :activated, as: :boolean

  h3 Branch Details
  .row-fluid
    .span4 = f.input :branch_address_line_1, label: &quot;Address 1&quot;
    .span4 = f.input :branch_address_line_2, label: &quot;Address 2&quot;
    .span4 = f.input :branch_address_line_3, label: &quot;Address 3&quot;
  .row-fluid
    .span4 = f.input :branch_state, label: &quot;State&quot;
    .span4 = f.input :branch_postcode, label: &quot;Postcode&quot;
    .span4
      = f.input :branch_country_code, label: &quot;Country&quot; do
        = f.country_select :branch_country_code, [&apos;au&apos;, &apos;us&apos;, &apos;gb&apos;], value: &apos;au&apos;
      - f.add_signed_fields :branch_country_code
  .row-fluid
    .span4 = f.input :branch_phone_number, label: &quot;Phone number&quot;
    .span4 = f.input :branch_fax_number, label: &quot;Fax number&quot;

  h3 Merchant Details
  .row-fluid
    .span4 = f.input :merchant_number
    .span4 = f.input :merchant_name
    .span4 = f.input :merchant_address
  .row-fluid
    .span4 = f.input :merchant_biller_code, label: &quot;Biller code&quot;
    .span4 = f.input :merchant_bpay_method, collection: Merchant::BPAY_METHODS.invert, label: &quot;BPAY method&quot;

  h3 Payment Details
  .row-fluid
    .span4 = f.input :payment_number
    .span4 = f.input :payment_merchant, label: &quot;Merchant description&quot;
    .span4 = f.input :payment_reference_type, collection: PaymentAccount::REFERENCE_TYPES.invert, label: &apos;Reference type&apos;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see, it’s a form containing details from bank account, branch address, branch contact information, merchant and payment. And indeed we do have &lt;code class=&quot;inline&quot;&gt;bank_accounts&lt;/code&gt;, &lt;code class=&quot;inline&quot;&gt;addresses&lt;/code&gt;, &lt;code class=&quot;inline&quot;&gt;contacts&lt;/code&gt;, &lt;code class=&quot;inline&quot;&gt;merchants&lt;/code&gt; and &lt;code class=&quot;inline&quot;&gt;payment_accounts&lt;/code&gt; tables in our database. Yet, the form still remains flat-structured and submits via &lt;code class=&quot;inline&quot;&gt;simple_form_for&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;
This is thanks to our &lt;code class=&quot;inline&quot;&gt;BankAccount&lt;/code&gt; entity and &lt;code class=&quot;inline&quot;&gt;BankAccountRepository&lt;/code&gt; repository:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;class BankAccount
  include Datamappify::Entity
  include Concerns::Archivable

  ACCOUNT_TYPES = {
    &apos;general&apos; =&gt; &apos;General&apos;,
    &apos;trust&apos; =&gt; &apos;Trust&apos;
  }

  attribute :account_type, String
  attribute :account_code, String
  attribute :account_name, String
  attribute :account_number, String
  attribute :bank_name, String
  attribute :bank_branch, String
  attribute :bank_bsb, String
  attribute :activated, Boolean, default: true

  attributes_from Address, prefix_with: :branch
  attributes_from Contact, prefix_with: :branch
  attributes_from Merchant, prefix_with: :merchant
  attributes_from PaymentAccount, prefix_with: :payment

  references :agency

  validates :account_type, Mos::Validation.in(ACCOUNT_TYPES)
  validates :account_code, Mos::Validation::STANDARD_TEXT
  validates :account_name, Mos::Validation::STANDARD_TEXT
  validates :account_number, Mos::Validation::STANDARD_TEXT
  validates :bank_name, Mos::Validation::STANDARD_TEXT
  validates :bank_branch, Mos::Validation::STANDARD_TEXT_OPTIONAL
  validates :bank_bsb, Mos::Validation::BSB
  validates :merchant_bpay_method, Mos::Validation.presence_depend_on(:merchant_biller_code)
  validates :payment_reference_type, Mos::Validation.presence_depend_on(:payment_number)
end

class BankAccountRepository
  include Datamappify::Repository

  for_entity BankAccount

  default_provider :ActiveRecord

  map_attribute :branch_address_line_1, &apos;ActiveRecord::Address#address_line_1&apos;
  map_attribute :branch_address_line_2, &apos;ActiveRecord::Address#address_line_2&apos;
  map_attribute :branch_address_line_3, &apos;ActiveRecord::Address#address_line_3&apos;
  map_attribute :branch_state, &apos;ActiveRecord::Address#state&apos;
  map_attribute :branch_postcode, &apos;ActiveRecord::Address#postcode&apos;
  map_attribute :branch_country_code, &apos;ActiveRecord::Address#country_code&apos;

  map_attribute :branch_phone_number, &apos;ActiveRecord::Contact#phone_number&apos;
  map_attribute :branch_fax_number, &apos;ActiveRecord::Contact#fax_number&apos;
  map_attribute :branch_email, &apos;ActiveRecord::Contact#email&apos;
  map_attribute :branch_website, &apos;ActiveRecord::Contact#website&apos;

  map_attribute :merchant_number, &apos;ActiveRecord::Merchant#number&apos;
  map_attribute :merchant_name, &apos;ActiveRecord::Merchant#name&apos;
  map_attribute :merchant_address, &apos;ActiveRecord::Merchant#address&apos;
  map_attribute :merchant_biller_code, &apos;ActiveRecord::Merchant#biller_code&apos;
  map_attribute :merchant_bpay_method, &apos;ActiveRecord::Merchant#bpay_method&apos;

  map_attribute :payment_number, &apos;ActiveRecord::PaymentAccount#number&apos;
  map_attribute :payment_merchant, &apos;ActiveRecord::PaymentAccount#merchant&apos;
  map_attribute :payment_reference_type, &apos;ActiveRecord::PaymentAccount#reference_type&apos;
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Pretty clean and straightforward. What do you think?&lt;/p&gt;
&lt;h3&gt;
Datamappify - Keeping Your Domain Layer Sane&lt;/h3&gt;
&lt;p&gt;
Datamappify’s concept isn’t new, but there simply isn’t anything in the ruby community that solves everything Datamappify tries to solve.&lt;/p&gt;
&lt;p&gt;
I hope that this post has not only introduced you to Datamappify, but has also made you think about how to make &lt;strong&gt;your&lt;/strong&gt; application’s domain layer more decoupled.&lt;/p&gt;
&lt;p&gt;
Thanks for reading, and if you have any feedback for Datamappify please get in touch with me!&lt;/p&gt;
]]&gt;</content>
    <published>2013-06-27T12:09:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/datamappify-a-new-take-on-decoupling-domain-form-and-persistence-in-rails"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/datamappify-a-new-take-on-decoupling-domain-form-and-persistence-in-rails</id>
    <title>Datamappify - A New Take on Decoupling Domain, Form and Persistence in Rails</title>
    <updated>2013-06-27T12:09:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
&lt;em&gt;Update: You might also want to check out &lt;a href=&quot;/blog/2010-05-25-rails-tip-model-attributes-not-updating/&quot;&gt;&lt;code class=&quot;inline&quot;&gt;reset_column_information&lt;/code&gt;&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
So a few days ago we started seeing the following errors on our Jenkins builds (swapped with fictional model and attribute names):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;NoMethodError:
  undefined method `attack_power=&apos; for #&lt;Ironman:0x00000008525d20&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;code class=&quot;inline&quot;&gt;attack_power&lt;/code&gt; is a new attribute we recently added to the &lt;code class=&quot;inline&quot;&gt;Ironman&lt;/code&gt; ActiveRecord model.&lt;/p&gt;
&lt;p&gt;
I was baffled, as the table column is clearly there but ActiveRecord couldn’t see it.&lt;/p&gt;
&lt;p&gt;
This weird behaviour is confirmed by debugging the model:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Ironman
# =&gt; Ironman(id: integer, created_at: datetime, updated_at: datetime)

Ironman.column_names
# =&gt; [&quot;id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
And by debugging the schema:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;ActiveRecord::Base.connection.structure_dump
# =&gt; &quot;CREATE TABLE `ironmans` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `created_at` datetime NOT NULL,\n `updated_at` datetime NOT NULL,\n `attack_power` int(11) NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n\n&quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
So apparently somehow ActiveRecord ate our &lt;code class=&quot;inline&quot;&gt;attack_power&lt;/code&gt; attribute!&lt;/p&gt;
&lt;p&gt;
After some struggling and head-scratching, finally I’ve discovered that during the migration if we call the &lt;code class=&quot;inline&quot;&gt;Ironman&lt;/code&gt; class, then any subsequent attribute changes to the class will not be recognised by ActiveRecord.&lt;/p&gt;
&lt;p&gt;
It turns out, because we run our test suites by invoking rake tasks:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Rake::Task[&apos;spec:something&apos;].invoke
Rake::Task[&apos;spec:something_else&apos;].invoke&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
And we manually reset our database beforehand too within the same &lt;code class=&quot;inline&quot;&gt;Rakefile&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Rake::Task[&apos;db:reset&apos;].invoke&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
ActiveRecord will cache its attributes as soon as a model class (&lt;code class=&quot;inline&quot;&gt;Ironman&lt;/code&gt;) is called during the migration.&lt;/p&gt;
&lt;p&gt;
The fix? Simple! Simply use another process to run the database reset:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;system &apos;rake db:reset&apos;&lt;/code&gt;&lt;/pre&gt;
]]&gt;</content>
    <published>2013-02-27T00:12:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/activerecord-and-db-migration-ate-my-model-attributes"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/activerecord-and-db-migration-ate-my-model-attributes</id>
    <title>ActiveRecord and DB Migration Ate My Model Attributes!</title>
    <updated>2013-02-27T00:12:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
Like a lot of places, at &lt;a href=&quot;http://locomote.com.au&quot;&gt;Locomote&lt;/a&gt; we are building a platform that is API-based. As much as I like having comprehensive test suites, I often feel the need to manually test API endpoints to see exactly what the responses are.&lt;/p&gt;
&lt;p&gt;
Tools such as &lt;a href=&quot;https://chrome.google.com/webstore/detail/fdmmgilgnpjigdojojpjoooidkmcomcm&quot;&gt;Postman&lt;/a&gt; solves part of the issue: they allow us to quickly test API endpoints without messing with &lt;a href=&quot;http://curl.haxx.se/&quot;&gt;cURL&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;
But as a lazy developer, I want more. ;)&lt;/p&gt;
&lt;p&gt;
I want something that:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
automatically generates API endpoints from Rails routes definition  &lt;/li&gt;
  &lt;li&gt;
defines input params as easy as defining routes  &lt;/li&gt;
  &lt;li&gt;
has input params that can be shared with test factories  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
And so &lt;a href=&quot;https://github.com/fredwu/api_taster&quot;&gt;API Taster&lt;/a&gt; was born. Please check it out to see how you can use it.&lt;/p&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/8Dnto.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
]]&gt;</content>
    <published>2012-07-02T04:59:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/api-taster-visually-test-rails-application-api"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/api-taster-visually-test-rails-application-api</id>
    <title>API Taster: Visually Test Rails Application API</title>
    <updated>2012-07-02T04:59:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
Ever wondered how you could utilise the &lt;code class=&quot;inline&quot;&gt;render&lt;/code&gt; method outside the context of Rails controllers and views? If you wonder why anyone would do that. Well, imagine you are building an awesome form builder, you need to output and/or store rendered partials in the buffer. How do you do that?&lt;/p&gt;
&lt;p&gt;
For example, what if you want to do this in your view?&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;&lt;%=raw Awesome::FormBuilder.new(some_options).html %&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
You could do something like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;module Awesome
  class FormBuilder &lt; AbstractController::Base
    include AbstractController::Rendering
    include ActionView::Context
    include ActionView::Helpers::CaptureHelper

    # set the view paths from your engine or from your application root, i.e. Rails.root
    self.view_paths = Awesome::Engine.root.join(&apos;app/views&apos;)

    def initialize(params)
      flush_output_buffer
      @_buffer = &apos;&apos;
      add_to_buffer(params)
    end

    def html
      @_buffer
    end

    private

    def add_to_buffer(params)
      # some logic to add rendered content to @_buffer
    end
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
The idea is to mixin the &lt;code class=&quot;inline&quot;&gt;render&lt;/code&gt; method, but also ensuring the view buffer is correctly reset with &lt;code class=&quot;inline&quot;&gt;flush_output_buffer&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;
Hope that helps. :)&lt;/p&gt;
]]&gt;</content>
    <published>2012-06-20T09:09:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/rails-tip-render-views-outside-of-controllers-or-views"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/rails-tip-render-views-outside-of-controllers-or-views</id>
    <title>[Rails Tip] Render views outside of Controllers or Views</title>
    <updated>2012-06-20T09:09:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
For the past few weeks I have started contributing to a small project - &lt;a href=&quot;http://github.com/stonean/slim&quot;&gt;Slim&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;
Slim is a fast, lightweight templating engine for Rails 3. It has been tested on Ruby 1.9.2 and Ruby/REE 1.8.7. Slim is heavily influenced by &lt;a href=&quot;http://github.com/nex3/haml&quot;&gt;Haml&lt;/a&gt; and &lt;a href=&quot;http://github.com/visionmedia/jade&quot;&gt;Jade&lt;/a&gt;.  &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;
&lt;a href=&quot;http://github.com/stonean&quot;&gt;Andrew Stone&lt;/a&gt; who is the author of the project has posted &lt;a href=&quot;http://stonean.com/slim-update&quot;&gt;a quick update&lt;/a&gt; on the latest feature additions to Slim. Please go check it out.&lt;/p&gt;
&lt;p&gt;
The source code of Slim is &lt;a href=&quot;http://github.com/stonean/slim&quot;&gt;available on Github&lt;/a&gt;.&lt;/p&gt;
]]&gt;</content>
    <published>2010-10-17T21:40:20.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/slim-a-fast-and-lightweight-rails-template-engine"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/slim-a-fast-and-lightweight-rails-template-engine</id>
    <title>Slim, a Fast and Lightweight Rails Template Engine!</title>
    <updated>2010-10-17T21:40:20.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
&lt;strong&gt;Before (Rails 3.0.1pre stable branch + Arel 1.0.1):&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_l9hgzuhG7b1qalr27.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;After (Rails 3.1.0 master branch + Arel 2.0.0dev master branch:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_l9hh02dWtv1qalr27.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;
Thanks to the awesome work done by Aaron Patterson (&lt;a href=&quot;http://github.com/tenderlove&quot;&gt;@tenderlove&lt;/a&gt;) and others. :-)&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;UPDATE:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;
After Aaron Patteron’s &lt;a href=&quot;http://twitter.com/tenderlove/status/25837698499&quot;&gt;tweet&lt;/a&gt;, I ran the tests again on Rails 3.0.1pre stable branch + Arel 2.0.0dev master branch, and the result blew my mind:&lt;/p&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_l9hhxw2fkl1qalr27.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
]]&gt;</content>
    <published>2010-09-29T00:51:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/a-speedier-rails-app-using-rails-3-1-arel-2-0"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/a-speedier-rails-app-using-rails-3-1-arel-2-0</id>
    <title>A Speedier Rails App using Rails 3.1 + Arel 2.0</title>
    <updated>2010-09-29T00:51:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
Don’t you just hate it when you get the following errors during a &lt;a href=&quot;http://www.capify.org/&quot;&gt;Capistrano&lt;/a&gt; deployment?&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;shell language-shell&quot;&gt;bundle: command not found
Could not find RubyGem bundler (&amp;gt;= 0) (Gem::LoadError)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
In fact, even if you don’t use &lt;a href=&quot;http://gembundler.com/&quot;&gt;bundler&lt;/a&gt;, you might still get errors like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;shell language-shell&quot;&gt;rake: command not found
Could not find RubyGem rake (&amp;gt;= 0) (Gem::LoadError)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
It turns out this has something to do with the &lt;code class=&quot;inline&quot;&gt;$PATH&lt;/code&gt; and &lt;code class=&quot;inline&quot;&gt;$GEM_HOME&lt;/code&gt; variables.&lt;/p&gt;
&lt;p&gt;
So here’s the quick fix.&lt;/p&gt;
&lt;p&gt;
Log in to your deployment server, as a root user, add the following line to &lt;code class=&quot;inline&quot;&gt;/etc/ssh/sshd_config&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;PermitUserEnvironment yes&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Don’t forget to restart &lt;code class=&quot;inline&quot;&gt;ssh&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;shell language-shell&quot;&gt;/etc/init.d/ssh restart&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Now, log in as the deployment user, and create ’~/.ssh/environment’ with the following content:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;shell language-shell&quot;&gt;PATH=/usr/local/rvm/gems/ruby-1.9.2-p0/bin:/bin:/usr/local/rvm/rubies/ruby-1.9.2-p0/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
GEM_HOME=/usr/local/rvm/gems/ruby-1.9.2-p0&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;em&gt;* The above paths are for your reference only, obviously you need to work them out for your server environment. The only thing you need to make sure is that the &lt;code class=&quot;inline&quot;&gt;GEM_HOME&lt;/code&gt;’s path matches one from the &lt;code class=&quot;inline&quot;&gt;PATH&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;
Now, to verify this all work, you may use &lt;code class=&quot;inline&quot;&gt;cap shell&lt;/code&gt; to start a new shell session and try out your commands.&lt;/p&gt;
]]&gt;</content>
    <published>2010-09-16T05:59:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/bundle-command-not-found-or-could-not-find-rubygem-bundler-0-during-capistrano-deployment-no-problems"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/bundle-command-not-found-or-could-not-find-rubygem-bundler-0-during-capistrano-deployment-no-problems</id>
    <title>`bundle: command not found` or `Could not find RubyGem bundler (&gt;= 0)` During Capistrano Deployment? No Problems!</title>
    <updated>2010-09-16T05:59:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
If you haven’t already been using &lt;a href=&quot;http://github.com/fredwu/app_config&quot;&gt;AppConfig&lt;/a&gt; in your Rails project, well, you should!&lt;/p&gt;
&lt;p&gt;
Jacques Crocker has recently released his new version of the original AppConfig - &lt;a href=&quot;http://github.com/railsjedi/rails_config&quot;&gt;&lt;strong&gt;RailsConfig&lt;/strong&gt;&lt;/a&gt;. I was invited to join the development of this new tool, so make sure you go &lt;a href=&quot;http://github.com/railsjedi/rails_config&quot;&gt;check it out&lt;/a&gt;. :-)&lt;/p&gt;
]]&gt;</content>
    <published>2010-08-11T01:50:14.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/rails-railsconfig-a-new-iteration-of-appconfig-for-rails-3"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/rails-railsconfig-a-new-iteration-of-appconfig-for-rails-3</id>
    <title>[Rails] RailsConfig, A New Iteration of AppConfig for Rails 3</title>
    <updated>2010-08-11T01:50:14.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
I have just &lt;a href=&quot;http://github.com/fredwu/inherited_resources_views/commit/5fd1f928a0ee1f31f413794b8569505ac072f814&quot;&gt;pushed a commit&lt;/a&gt; that added Rails 2.x compatibility to &lt;a href=&quot;http://github.com/fredwu/inherited_resources_views&quot;&gt;Inherited Resources Views&lt;/a&gt;. Please give it a spin! :-)&lt;/p&gt;
&lt;p&gt;
P.S. I’ve only tested it on Rails 2.3.8.&lt;/p&gt;
]]&gt;</content>
    <published>2010-08-07T07:35:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/rails-inherited-resources-views-now-supports-rails-2-x"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/rails-inherited-resources-views-now-supports-rails-2-x</id>
    <title>[Rails] Inherited Resources Views Now Supports Rails 2.x</title>
    <updated>2010-08-07T07:35:00.000000Z</updated>
  </entry>
</feed>