Model(テーブル)の関連づけ

RailsActiveRecordは、一対一、一対多、多対多のテーブルの関連を扱う事ができます。その為にはActiveRecord に関連を定義します。

今回のテーブル構成を見ると、articles と users は多対多の関係を持っています。しかし、その関係は requests テーブルを経由する 「情報と翻訳希望者の関係」と、translations テーブルを経由する 「情報と翻訳者の関係」の2つがあります。
ActiveRecord にはこのような関係も has_many 関係名, :through => 関連テーブル, :source => 関連先テーブル で扱う事ができます。 エライ ^^)/

class Article < ActiveRecord::Base
  has_many :requests
  has_many :translations
  has_many :requesters, :through => :requests, :source => :user
  has_many :traslators, :through => :translations, :source => :user
end

class User < ActiveRecord::Base
  has_many :requests
  has_many :translations
  has_many :requested_articles, :through => :requests, :source => :article
  has_many :traslated_articles, :through => :translations, :source => :article
  has_one  :setup
end

class Request < ActiveRecord::Base
  belongs_to :article
  belongs_to :user
end

class Translation < ActiveRecord::Base
  belongs_to :article
  belongs_to :user
  has_many :appreciations
  has_many :users, :through => :appreciations
end

class Setup < ActiveRecord::Base
  belongs_to :user
end