[英]Variable in active record query, ruby on rails 4
I try to pass in a variable in a active record query: 我尝试在活动记录查询中传递变量:
class UsersController < ApplicationController
def show
@user = User.find_by_username(params[:username])
@tags = @user.tag_list
i = 0
@tags.each do |tag|
i= i + 1
@tag"#{i}" = Info.tagged_with(tag.name)
end
end
end
As you can see, I try to pass in a variable in @tag. 如您所见,我尝试在@tag中传递变量。 I tried
@tag"#{i}"
, but this doesn't work. 我尝试了
@tag"#{i}"
,但这不起作用。 What do I need to type in, to get @tag
with the number i? 我需要输入什么才能获得带有数字i的
@tag
? My goal is the following output: 我的目标是以下输出:
@tag1
@tag2
@tag3
Thanks in advance! 提前致谢!
您可以使用instance_variable_set()
方法:
instance_variable_set(:"@tag#{i}", Info.tagged_with(tag.name))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.