0

ユーザーの雇用と会社のどこにいるかを追跡するアプリを作成しています。アプリをルーティングするのに助けが必要です。 usercompany、およびの足場を作成しdepartmentました。

  • user
    • company(ユーザー has_many :through => 雇用)
      • department

user.rb

class User < ActiveRecord::Base
    #associations
    has_many :employments
    has_many :companies, :through => :employments
    has_one :department, :through => :employments
end

雇用.rb

class Employment < ActiveRecord::Base

  belongs_to :user
  belongs_to :company
  belongs_to :department
  has_many   :employment_histories
end

雇用履歴.rb

class EmploymentHistory < ActiveRecord::Base

  belongs_to :employment
end

company.rb

class Company < ActiveRecord::Base

  has_many :employments
  has_many :users, :through => :employments
  has_many :departments
end

部門.rb

class Department < ActiveRecord::Base
  belongs_to :company
end
4

1 に答える 1

0

ルーティングの場合、最も簡単な方法は、リソースをリソースとして宣言し、オブジェクトの ID を介してそれらにアクセスすることです。 http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

次に、関連付けを通じて見つけるには、このレールキャストをチェックすることをお勧めします: http://railscasts.com/episodes/3-find-through-association?view=asciicast

関連付けによるルーティングと検索が容易でない場合は、比喩的により意味のある階層をリファクタリングすることをお勧めします。たとえば、私のアプローチは次のようになります。

  • ユーザーには has_many :employments を指定し、ユーザー モデルで default_scope order: 'employments.created_at DESC' を指定して、最新の雇用が最初になるようにします。
  • 雇用 has_one :部門
  • 部署の所属先:会社
  • 会社 has_many :部門

次に、関連付けを介してこのすべてのデータにアクセスできます (上記のリンクを参照)。リソースをルーティングするだけで、適切なコントローラーでそれらにアクセスする方法を知ることができます。

于 2012-07-10T22:33:39.813 に答える