微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 需要来自rails的数据连接表,has_many:through

我有3个表 – 用户,事情和跟随.用户可以通过以下表格来跟踪事件,将user_id与things_id相关联.这意味着:
class User
  has_many :things,:through => :follows
end

class Thing
  has_many :users,:through => :follows
end

class Follow
  belongs_to :users
  belongs_to :things
end

所以我可以检索thing.users没有问题.我的问题是,如果在下面的表中,我有一个名为“关系”的列,所以我可以设置一个追随者作为“管理员”,我想要访问该关系.所以在循环中我可以做一些像:

<% things.users.each do |user| %>
  <%= user.relation %>
<% end %>

有没有办法将关系包含在原始用户对象中?我试过:select => “follow.relation”,但它似乎不加入属性.

解决方法

为此,您需要在has_many中使用一些sql.这样的事情应该有希望的工作.
has_many:users,:through =>如下:select => ‘users.*,follow.is_admin as is_follow_admin’

然后在循环中你应该可以访问user.is_follow_admin

原文地址:https://www.jb51.cc/ruby/272133.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐