【发布时间】:2012-02-11 01:56:28
【问题描述】:
我有 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 => "follows.relation",但是好像没有加入属性。
【问题讨论】:
-
将关系命名为“关系”是一个非常糟糕的主意...这可能会破坏一些 ruby 内部结构,您会得到意想不到的行为!
-
好的,谢谢。假设该列名为“is_admin”并且具有布尔类型..
标签: ruby-on-rails associations has-many-through has-many