如何将多个资源合并为一个Feed?

时间:2015-03-13 23:19:45

标签: ruby-on-rails ruby methods feed

我们如何将所有这些资源合并到一个Feed中,最新提交的内容显示在顶部?

user.rb

  # Returns status feed.
  def feed
    following_ids = "SELECT followed_id FROM relationships
                     WHERE  follower_id = :user_id"
    Habit.where("user_id IN (#{following_ids})
                     OR user_id = :user_id", user_id: id)
    Valuation.where("user_id IN (#{following_ids})
                     OR user_id = :user_id", user_id: id)
    Goal.where("user_id IN (#{following_ids})
                     OR user_id = :user_id", user_id: id)
    Quantified.where("user_id IN (#{following_ids})
                     OR user_id = :user_id", user_id: id)
  end

schema.rb

ActiveRecord::Schema.define(version: 20150311202504) do

  create_table "authentications", force: true do |t|
    t.integer  "user_id"
    t.string   "provider"
    t.string   "uid"
    t.string   "index"
    t.string   "create"
    t.string   "destroy"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "days", force: true do |t|
    t.integer  "level_id"
    t.integer  "habit_id"
    t.boolean  "missed",     default: false
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
  end

  create_table "goals", force: true do |t|
    t.string   "name"
    t.date     "deadline"
    t.boolean  "accomplished"
    t.text     "comment"
    t.boolean  "private_submit"
    t.integer  "user_id"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
  end

  add_index "goals", ["user_id", "created_at"], name: "index_goals_on_user_id_and_created_at"
  add_index "goals", ["user_id"], name: "index_goals_on_user_id"

  create_table "habits", force: true do |t|
    t.datetime "left"
    t.integer  "level"
    t.text     "committed"
    t.datetime "date_started"
    t.string   "trigger"
    t.string   "target"
    t.string   "reward"
    t.text     "comment"
    t.boolean  "private_submit"
    t.integer  "user_id"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
  end

  add_index "habits", ["user_id", "created_at"], name: "index_habits_on_user_id_and_created_at"
  add_index "habits", ["user_id"], name: "index_habits_on_user_id"

  create_table "levels", force: true do |t|
    t.integer  "user_id"
    t.integer  "habit_id"
    t.integer  "days_needed"
    t.boolean  "passed",      default: false
    t.datetime "created_at",                  null: false
    t.datetime "updated_at",                  null: false
  end

  create_table "quantifieds", force: true do |t|
    t.string   "categories"
    t.string   "metric"
    t.text     "comment"
    t.boolean  "private_submit"
    t.integer  "user_id"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
  end

  add_index "quantifieds", ["user_id", "created_at"], name: "index_quantifieds_on_user_id_and_created_at"
  add_index "quantifieds", ["user_id"], name: "index_quantifieds_on_user_id"

  create_table "relationships", force: true do |t|
    t.integer  "follower_id"
    t.integer  "followed_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id"
  add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
  add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id"

  create_table "results", force: true do |t|
    t.integer  "user_id"
    t.string   "result_value"
    t.date     "date_value"
    t.integer  "quantified_id"
    t.boolean  "good"
    t.text     "comment"
    t.datetime "created_at",    null: false
    t.datetime "updated_at",    null: false
  end

  create_table "taggings", force: true do |t|
    t.integer  "tag_id"
    t.integer  "taggable_id"
    t.string   "taggable_type"
    t.integer  "tagger_id"
    t.string   "tagger_type"
    t.string   "context",       limit: 128
    t.datetime "created_at"
  end

  add_index "taggings", ["tag_id", "taggable_id", "taggable_type", "context", "tagger_id", "tagger_type"], name: "taggings_idx", unique: true
  add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context"

  create_table "tags", force: true do |t|
    t.string  "name"
    t.integer "taggings_count", default: 0
  end

  add_index "tags", ["name"], name: "index_tags_on_name", unique: true

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.text     "missed_days"
    t.text     "missed_levels"
    t.string   "provider"
    t.string   "uid"
    t.string   "oauth_token"
    t.datetime "oauth_expires_at"
    t.datetime "created_at",                        null: false
    t.datetime "updated_at",                        null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.boolean  "admin",             default: false
    t.string   "activation_digest"
    t.boolean  "activated",         default: false
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.datetime "reset_sent_at"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true

  create_table "valuations", force: true do |t|
    t.string   "name"
    t.text     "comment"
    t.boolean  "private_submit"
    t.integer  "user_id"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
  end

  add_index "valuations", ["user_id", "created_at"], name: "index_valuations_on_user_id_and_created_at"
  add_index "valuations", ["user_id"], name: "index_valuations_on_user_id"

end

pages_controller.rb

class PagesController < ApplicationController
  def home
    if logged_in?
    	@habits = current_user.habits.build
	    @valuations = current_user.valuations.build
      @accomplished_goals = current_user.goals.accomplished
      @unaccomplished_goals = current_user.goals.unaccomplished
      @averaged_quantifieds = current_user.quantifieds.averaged
      @instance_quantifieds = current_user.quantifieds.instance
	    @feed_items = current_user.feed.paginate(page: params[:page])
  	end
   end


  def about 
	end
end

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

非常感谢您的时间!到目前为止,Infused的答案无效。

1 个答案:

答案 0 :(得分:1)

处理它的最简单方法是将集合与+

组合在一起
cond = ["user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id]

Habit.where(cond) + Valuation.where(cond) + Goal.where(cond) + Quantified.where(cond)

作为评论中提到的Matt Brictson,可能有更好的方法来聚合各种模型,但只需使用+加入集合即可。