ユーザーが保存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追加したかどうかを示す必要があります。現時点では、呼び出しはデータベースからすべてを取得していますが、これは非効率的です。他のレコードは必要ないため、この呼び出しをフィルタリングして、に属するお気に入りのみをフェッチする方法が必要です。Entryfavourite@entry.fansUsercurrent_user
コントローラーのメモリ内でこれを行うことができますが、current_userお気に入りを選択して、 EntriesActive::Record を使用してモデルに結合する方法があると考えています。
ありがとう。