5

Given this code:

has_many :foos, :finder_sql = <<-SQL
  select * from foos where bars = #{id}
SQL

The #{id} part is being prematurely interpolated.

How do I escape it?

3
  • yes because ruby interprets as a comment Commented Jan 12, 2010 at 21:58
  • @streetparade: Not true. HereDocs do not have comments... in fact, their entire purpoase is so that you can ignore formatting. However, they act like double-quoted strings (So #{code} is executed). mckeed's method tells ruby to treat the HereDoc like a single-quoted string. Commented Jan 12, 2010 at 23:34
  • see also railsblaster.wordpress.com/2007/08/27/has_many-finder_sql Commented Jan 13, 2010 at 9:58

1 Answer 1

12

Put single quotes around the delimiter:

has_many :foos, :finder_sql = <<-'SQL'
  select * from foos where bars = #{id}
SQL
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.