خرید بک لینک

Vote count: 0

I'm using the acts_as_follower gem in addition to the polymorphic activity tracking I've setup between my user and artist models. I'm now trying to display all of the activities of followed artists in the user's show view. At first I thought I could simply create an instance variable of artists by using the gem's all_follows method:

current_user.all_follows
=> #<ActiveRecord::AssociationRelation [#<Follow id: 8, followable_id: 1, followable_type: "Artist", follower_id: 1, follower_type: "User", blocked: false, created_at: "2016-02-29 12:56:31", updated_at: "2016-02-29 12:56:31">]>

As you can see it retus the Follow object(s) vs. the Artist objects I'm looking to retrieve. I had an alteative idea by performing a sql query on the Activity class:

Activity.where(followable_id: current_user.follows.pluck(&:id))

This gives me the error:

SQLite3::SQLException: no such column: activities.followable_id: SELECT "activities".* FROM "activities" WHERE "activities"."followable_id" IN (8, 1, 'Artist', 1, 'User', 'f', '2016-02-29 12:56:31.974244', '2016-02-29 12:56:31.974244')
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: activities.followable_id: SELECT "activities".* FROM "activities" WHERE "activities"."followable_id" IN (8, 1, 'Artist', 1, 'User', 'f', '2016-02-29 12:56:31.974244', '2016-02-29 12:56:31.974244')

What would be the best way to retrieve these records?

class Artist < ActiveRecord::Base
  has_many :activities, as: :owner, dependent: :destroy
  acts_as_followable
end

class User < ActiveRecord::Base
  acts_as_follower
end

class Activity < ActiveRecord::Base
  belongs_to :trackable, polymorphic: true
  belongs_to :owner, polymorphic: true
end

class Follow < ActiveRecord::Base
  extend ActsAsFollower::FollowerLib
  extend ActsAsFollower::FollowScopes

  belongs_to :followable, polymorphic: true
  belongs_to :follower, polymorphic: true
end


class UsersController < ApplicationController
  Activity.where(followable_id: current_user.follows.pluck(&:id))
end

asked 4 mins ago

- - , .

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 10 اسفند 1394 ساعت: 20:30

صفحه بندی