activerecord, refactoring the source code I have three tables

class User &lt; AR::Base
  has_many :lexemes, :dependent =&gt; :destroy
end

class Lexeme &lt; AR::Base
  belongs_to :user
  belongs_to :word
end

class Word &lt; AR::Base
  has_many :lexemes
end


This code is storing the word to Words table and assign it to the user.

word = Word.where(:word=&gt;token).first_or_create
lexeme = @user.lexemes.where(:word_id=&gt;word.id).first_or_create


I don't like my code, so is it possible to improve it? Thanks
class User &lt; AR::Base
  has_many :lexemes, :dependent =&gt; :destroy
  has_many  :words, :through =&gt; :lexemes
end

class Lexeme &lt; AR::Base
  belongs_to :user
  belongs_to :word
end

class Word &lt; AR::Base
  has_many :lexemes
  has_many :users, :through =&gt; :lexemes
end


You should normally be able to create word by following code but it seems there is a bug in Rails.

word = @user.words.find_or_create_by_word(token)


Therefore I have changed the model to following. You may remove it when the bug is fixed.

class User &lt; ActiveRecord::Base
  has_many :lexemes
  has_many :words, :through =&gt; :lexemes do
        def find_or_create_by_word(token)
            find_by_word(token) || create(:word =&gt; token)
        end
  end
end