ruby on rails - Creating a feed => NoMethodError: undefined method for ActiveRecord_Associations_CollectionProxy -
i implementing feed of cards belonging tags user has subscribed , following error. it's trivial can't pinpoint needs work.
nomethoderror: undefined method `cards' tag::activerecord_associations_collectionproxy:0x007fbaa46239f8>
here models:
class user < activerecord::base has_many :cards, dependent: :destroy has_many :tags, through: :cards has_many :subscriptions, dependent: :destroy has_many :subscribed_tags, through: :subscriptions, source: :tag end class tag < activerecord::base has_many :taggings, dependent: :destroy has_many :cards, through: :taggings has_many :subscriptions, dependent: :destroy has_many :subscribers, through: :subscriptions, source: :user end class card < activerecord::base acts_as_votable belongs_to :user has_many :taggings, dependent: :destroy has_many :tags, through: :taggings def self.tagged_with(name) tag.find_by_name!(name).cards end def self.tag_counts tag.select("tags.*, count(taggings.tag_id) count"). joins(:taggings).group("taggings.tag_id") end def tag_list tags.map(&:name).join(", ") end def tag_list=(names) self.tags = names.split(",").map |n| tag.where(name: n.strip).first_or_create! end end end
what trying run current_user.subscribed_tags.cards , retrieve array of cards can reorder , output timeline.
thanks
subscribed_tags
- it's scope (where(user: self)
), can call where
or join
on them not item methods.
in case want use scope
class card scope :with_subscription, -> { joins(tags: :subscriptions) } end # in controller current_user.cards.with_subscription.order('cards.created_at desc')
you can imagine current_user.cards
form of cards.where(user: current_user)
. once tell retrieve card
array - cannot changed. cannot user.cards.subscriptions
or user.where(id: user).cards.tags
can it's filter.
next filter joins(:subscriptions)
. give inner join, cards belonged user have subscriptions. , it's scope can modify further passing example order.
Comments
Post a Comment