現在、私のホームページには、記事、フォーラムであるさまざまなモデルのデータが表示されています。
私が書いたコードは、異なるモデルからのデータを示していますが、それらが作成された日時に従っていません。つまり、記事のデータは日付と時刻に応じて表示されます。投票と同じですが、全体としては表示されません。つまり、記事が投票の前に作成された場合、投票の前に表示されません。
これは、ポーリング後に記事のコードを記述し、コントローラーでさまざまな変数を使用してさまざまなモデルからデータを取得し、ビューでこれらの変数を次のようにループしているためです。
このホームページのコントローラのコードは
def index
@col = Education.select(:college_id).where(user_id: @current_user.id).all
@users = Education.select(:user_id).where(college_id: @col.collect(&:college_id)).all
@poll = Poll.where(created_by: @users.collect(&:user_id)).all
@article = Article.where(user_id: @users.collect(&:user_id)).all
end
そしてインデックスビューページで私は次のコードを書いています
- unless @poll.empty?
- @poll.reverse.each do | poll|
#statusbox
.imgbox
- if User.find(poll.created_by).basic_info
- if User.find(poll.created_by).basic_info.profilephoto?
= image_tag User.find(poll.created_by).basic_info.profilephoto.url(:thumb)
- else
%span no img
- else
%span no img
#welcomeheader
%span.bl=User.find(poll.created_by).first_name
posted a poll
%span.bl= time_ago_in_words(poll.created_at)
ago
%p=link_to poll.name,poll
%br
- unless @article.empty?
- @article.reverse.each do | article|
#statusbox
.imgbox
- if User.find(article.user_id).basic_info
- if User.find(article.user_id).basic_info.profilephoto?
= image_tag User.find(article.user_id).basic_info.profilephoto.url(:thumb)
- else
%span no img
- else
%span no img
#welcomeheader
%span.bl=User.find(article.user_id).first_name
posted an article
%span.bl= time_ago_in_words(article.created_at)
ago
%p=link_to article.name,article
%br
これらの異なるモデルからのデータを保存し、それを単一の変数に保存して、それをループする方法、または単一のビューで他のモデルからの日付と時間ごとの更新を表示できる他の方法はありますか