加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:52:43 所属栏目:百科 来源:网络整理
导读:我有3个表 – 用户,事情和跟随.用户可以通过以下表格来跟踪事件,将user_id与things_id相关联.这意味着: class User has_many :things,:through = :followsendclass Thing has_many :users,:through = :followsendclass Follow belongs_to :users belongs_to
我有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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读