Ruby on Rails 3.2.2 を使用していますが、関連するオブジェクトを処理するための一般的な方法を知りたいですhas_many :through ActiveRecord::Association。つまり、私は持っています:
class User < ActiveRecord::Base
has_many :article_editor_associations, :class_name => 'Articles::UserEditorAssociation'
has_many :articles, :through => :article_editor_associations
end
class Article < ActiveRecord::Base
has_many :user_editor_associations, :class_name => 'Articles::UserEditorAssociation'
has_many :editor_users, :through => :user_editor_associations
end
class Articles::UserAssociation < ActiveRecord::Base
belongs_to :editor_users
belongs_to :articles
end
上記のコードを使用することで、メソッドを実行してEditorのを@article.editor_users取得できます。ただし、アプリケーションにより良く適合させるために (つまり、たとえば、I18n 翻訳などを「プログラムによる」方法で処理するために)、次のような新しいモデルをアプリケーションに追加することを考えています。以下:Array User
class EditorUser < User # Note the class name and the class inheritance
...
end
このようにして、私のアプリケーションを通じて、EditorUser記事の「エディター ユーザー」インスタンスをUserオブジェクトであるかのように処理するために、クラスを参照できEditorUserます。 scope メソッド) インスタンスのみで利用可能EditorUser(インスタンスではなくUser)...
私の場合、作りたいものを作るのは「一般的な」アプローチですか?それは「Rails Way」ですか?もしそうなら、この状況を処理するために私は何ができる/すべきですか? いいえの場合、どのように進めればよいですか?
EditorUser < User ... endつまり、関連付けられたオブジェクト (メソッドEditorUserを実行して取得) はオブジェクトであるため、クラスを使用することを考えました。ディレクトリ (または他の場所) でクラスを指定することで、アプリケーション内の作業を簡素化できると思います。これは、その定数名を回避できるためです (たとえば、翻訳文字列を「構築」するためにその定数名を「再生」したり、インスタンスに使用するためだけに新しいメソッドを記述することによって)。@article.editor_usersUserEditoUserapp/modelsEditorUser