Ruby on Rails 3,我正在尝试存储一个变量,其记录的属性为:attend值为“ Yes”,但也有一个user_id属于一个reseller_id。
经销商有许多用户。证书属于一个用户。我可以根据证书中的:user_id属性获取用于存储用户和/或代理商的变量,但无法根据证书属性值(“是”或“否”)弄清楚该变量是否要存储
控制器定义变量:
@certs = Certificate.all
@reseller_users_incerts = []
@trained_user_id = []
@certs.each do |user|
#if user.find_by_attend("Yes")
@trained_user_id << user.user_id #all user ids in certification table
id = user.user_id
if u = User.find(id)
@reseller_users_incerts << u.reseller_id #all reseller ids from certifications table
end
#end
end
@reseller_users_incerts为我提供了所有保存了证书记录的用户的所有经销商的ID。我还需要基于证书的证书存储变量:出席值“是”或“否”
这是视图:
<% if @reseller_users_incerts.include?(reseller.id) %>
<td><%= "Yes" %></td>
<% else %>
<td><%= "No" %></td>
<% end %>
证书型号:
class Certificate < ActiveRecord::Base
attr_accessible :attend, :pass, :user_id
belongs_to :user
validates :user_id, presence: true
end
谢谢
您的内部块令人困惑,因为user
当它实际上是一个时,您将其用作循环变量Certificate
。更改该变量并假定它attend
确实是的属性Certificate
,然后使用您现有的结构引入此检查,如下所示:
@certs.each do |cert|
if cert.attend == "Yes"
@trained_user_id << cert.user_id #all user ids in certification table
id = cert.user_id
if u = User.find(id)
@reseller_users_incerts << u.reseller_id #all reseller ids from certifications table
end
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句