【发布时间】:2015-06-14 17:49:52
【问题描述】:
我正在实现属于用户订阅的标签的所有卡片的提要,我收到以下错误。这可能是微不足道的事情,但我无法确定需要做什么。
NoMethodError: undefined method `cards' Tag::ActiveRecord_Associations_CollectionProxy:0x007fbaa46239f8>
这是我的模型:
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) as 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 do |n|
Tag.where(name: n.strip).first_or_create!
end
end
end
我真正想做的是运行 current_user.subscribed_tags.cards 并检索我可以重新排序并作为时间线输出的卡片数组。
谢谢
【问题讨论】:
-
您要检索 current_user 的标签数组或 current_user 的 subscribed_tags 卡片数组?
-
糟糕,错字:current_user 的 subscubed_tags 卡片数组
标签: ruby-on-rails rails-activerecord has-many-through model-associations rails-postgresql