ana
ana

Reputation: 623

Concatenation on rails4

I would like to ask a question about concatenation on rails4. I want to concatenate the two(2) inputted data and save to the database. These are my codes:

 a = playernum
 b = playername
 ref = "{#{a} #{b}}"
 s.player_id = ref
 sum_pts = Player.where(playernum: playernum).sum(:pts)
 sum_game = Player.where(playernum: playernum).sum(:gp)
 s.apts = sum_pts / sum_game
 s.save 

the playernum and playername are the inputted data. and I want to concatenate them and store to the player_id and save to database.

Thanks in advance.

Upvotes: 1

Views: 74

Answers (1)

jeffdill2
jeffdill2

Reputation: 4114

If all you want to know is how to concatenate strings:

s.player_id = "#{playernum}#{playername}"

will do the trick.

In your example, the extra curly braces you have surrounding your variables will actually be part of the string (which I would assume you don't want). So with your code, the output would look like this:

"{12345ana}"

FWIW, there's also no need to store your variables into another variable before concatenating - i.e. assigning a to playernum.

Upvotes: 1

Related Questions