新手问题 关于引用地址问题

a4652097 · October 14, 2014 · Last by hz_qiuyuanxin replied at October 14, 2014 · 1453 hits

代码如下 span.badge = article.comment.content 报错: undefined method `content' for #Comment::ActiveRecord_Associations_CollectionProxy:0x007fe9f46ca4e0 什么问题呢?

额额额额

article 和 comment 是 has_many 的关系吧? 你这样是想取出什么呢? 所有

article.comments.map { |comment| comment.content }

某个 comment

article.comments.find(id).content

#3 楼 @loveltyoic 按照你说的成功了,好开心,那我想求出 comment 的和呢?

#4 楼 @a4652097 comment 求和是指什么? 如果是想知道多少条 comments 的话,article.comments.size。 楼主需要补补基础,看 tutorial 那本书吧。

竟然用 article.comments.map { |comment| comment.content } 而不是 article.comments.pluck(:content)

You need to Sign in before reply, if you don't have an account, please Sign up first.