尝试创建数组或具有ID和名称但
@campaign_ids_and_names = Hash.new
@campaign_ids.each do |cid|
@campaign_ids_and_names.merge!(cid => Campaign.find(cid, :user => current_user).name)
end
@campaign_ids_and_names
一直只返回1
我知道在Campaign表中确实存在ID。 我认为问题更多的是我如何从另一个创建1个数组或哈希的知识。
非常感谢任何帮助。
答案 0 :(得分:1)
尝试这样的事情:
@campaign_ids_and_names = Hash.new
@campaign_ids.each do |cid|
@campaign_ids_and_names[cid] = Campaign.find(cid, :user => current_user).name
end
@campaign_ids_and_names
您还应该检查Active Record关联:http://guides.rubyonrails.org/association_basics.html
这将帮助您清理您尝试做的更多内容,如:
@campaign_ids_and_names = Hash.new
@campaign_ids.each do |cid|
@campaign_ids_and_names[cid] = current_user.campaigns.find(cid).name
end
@campaign_ids_and_names