2015-03-13 3 views
-1

Как мы можем объединить все эти ресурсы в один канал, с последним представлением вверху?Как объединить несколько ресурсов в один канал?

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? 
 
    \t @habits = current_user.habits.build 
 
\t  @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 
 
\t  @feed_items = current_user.feed.paginate(page: params[:page]) 
 
    \t end 
 
    end 
 

 

 
    def about 
 
\t end 
 
end

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

спасибо за ваше время! До сих пор ответ Infused не работает.

+1

Чтобы получить желаемый результат, вам может потребоваться создать отдельную модель «активность» для агрегирования всех данных в одну таблицу, которую вы можете легко запросить. Ознакомьтесь с [RailsCasts # 407] (http://railscasts.com/episodes/407-activity-feed-from-scratch) для прохода. –

+0

Я бы использовал модель с поддержкой вида [(читайте об этом здесь)] (http://dan.chak.org/enterprise-rails/chapter-11-view-backed-models/) – Mario

ответ

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 упоминалось в комментариях, может быть лучший способ для объединения различных моделей, а просто используя + присоединиться коллекции сделают трюк.

+0

Спасибо, Infused! Я добавил ваш код в 'def feed end' и получил эту ошибку: ActiveRecord :: StatementInvalid в PagesController # home SQLite3 :: SQLException: нет такого столбца:: SELECT" habits ". * FROM" habits "WHERE (user_id IN ([]) ИЛИ user_id = 1). У вас есть идея, где мы поступили не так? –

+0

Похоже, что таблица 'habits' не имеет столбец' user_id'. – infused

+0

Если я удалю «Habit», это также произойдет для «Оценки» и т. Д. И т. Д. Все они имеют «user_id». –

Смежные вопросы