繁体   English   中英

如何获得我所关注书籍的作者姓名?

[英]How can I have the names of the authors of my followed books?

在user.rb(模型)中,我有:has_many:followed,class_name:在has_many:followed_books之后,通过::followed,源::book

在followed.rb中,我有书的ID和用户的ID。 在book.rb中,我还有作者(用作者的名字)

如何获得我所关注书籍的作者姓名?

这是正确的吗:current_user.followed_books(:author)还是仅current_user.followed_books.author?

如果'author'是代表作者姓名的属性,则可以像这样访问名称:

current_user.followed_books.pluck(:author)

如果“作者”是一个对象:

current_user.followed_books.includes(:author).pluck("authors.name")

如果你想要的ID

current_user.followed_books.includes(:author).pluck("authors.id")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM