Metaphysiker
Metaphysiker

Reputation: 1093

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. I tried @tag"#{i}", but this doesn't work. What do I need to type in, to get @tag with the number i? My goal is the following output:

@tag1
@tag2
@tag3

Thanks in advance!

Upvotes: 0

Views: 118

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44380

You can use the instance_variable_set() method:

instance_variable_set(:"@tag#{i}", Info.tagged_with(tag.name))

Upvotes: 1

Related Questions