Ruby on rails 检查加入视图
我有点像Ruby on rails 检查加入视图,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有点像 <% @users.each do |u| %> ... <% @interests.each do |i| %> <% if i joins u in interests_users %> <-- That is not working that way! = Problem ... <% i.id %> ... <% end %> <% end %
<% @users.each do |u| %>
...
<% @interests.each do |i| %>
<% if i joins u in interests_users %> <-- That is not working that way! = Problem
...
<% i.id %>
...
<% end %>
<% end %>
...
<% end %>
...
您认为这太以视图为中心了-这是一个数据问题,因此模型应该处理它
我猜你在用户和兴趣之间有一个N-M关系,用户的兴趣在中间加入表格。模型应该定义这样的关系:
class User < ActiveRecord::Base
has_and_belongs_to_many :interests
end
class Interest ...
has_and_belongs_to_many :users
end
编辑:
哦,是的,如果您想列出所有兴趣并标记用户关联的兴趣,类似这样的方式应该可以:
@interests.each do |interest|
if u.interests.include?(interest) ...
你能更详细地说明这个问题吗?你想达到什么目的?这确实很简单。未考虑在相关变量:/facepalm之后使用.each
@users = User.includes(:interests).all
@interests.each do |interest|
if u.interests.include?(interest) ...