ユーザーが保存Favorite
できるモデルがありEntries
ます。Feeds
class User < ActiveRecord::Base
has_many :favorites
has_many :favorite_feeds, :through => :favorites, :source => :favorable, :source_type => "Feed"
has_many :favorite_entries, :through => :favorites, :source => :favorable, :source_type => "Entry"
end
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :favorable, :polymorphic => true
attr_accessible :user, :favorable
end
class Feed < ActiveRecord::Base
has_many :favorites, :as => :favorable
has_many :fans, :through => :favorites, :source => :user
end
class Entry < ActiveRecord::Base
has_many :favorites, :as => :favorable
has_many :fans, :through => :favorites, :source => :user
end
Entries
ページにALL を表示し、 がそれぞれを としてcurrent_user
追加したかどうかを示す必要があります。現時点では、呼び出しはデータベースからすべてを取得していますが、これは非効率的です。他のレコードは必要ないため、この呼び出しをフィルタリングして、に属するお気に入りのみをフェッチする方法が必要です。Entry
favourite
@entry.fans
User
current_user
コントローラーのメモリ内でこれを行うことができますが、current_user
お気に入りを選択して、 Entries
Active::Record を使用してモデルに結合する方法があると考えています。
ありがとう。