-1

現在、私のホームページには、記事、フォーラムであるさまざまなモデルのデータが表示されています。

私が書いたコードは、異なるモデルからのデータを示していますが、それらが作成された日時に従っていません。つまり、記事のデータは日付と時刻に応じて表示されます。投票と同じですが、全体としては表示されません。つまり、記事が投票の前に作成された場合、投票の前に表示されません。

これは、ポーリング後に記事のコードを記述し、コントローラーでさまざまな変数を使用してさまざまなモデルからデータを取得し、ビューでこれらの変数を次のようにループしているためです。

このホームページのコントローラのコードは

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

これらの異なるモデルからのデータを保存し、それを単一の変数に保存して、それをループする方法、または単一のビューで他のモデルからの日付と時間ごとの更新を表示できる他の方法はありますか

4

1 に答える 1

1

コードで見たように、4つのモデルすべてで同じタイプのデータを表示しているため、テーブルレスモデルを作成できます

class PostWrapper
    store_in nil
    belongs_to :user, :inverse_of => nil

    field :created_at, type: Time
    field :content, type: String
    filed :post_type, type: String
    and all other fields
end

これらのモデルのオブジェクトに値を割り当てて、ビューで使用できます

または

4 つのモデルすべてに共通の属性と動作がある場合は、STI (単一テーブル継承) リンク (http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html) を試すことができます。 )、そのテーブルのオブジェクトを使用し、それに応じてビューで使用します。

于 2012-05-15T09:37:41.207 に答える