2010-01-12 8 views
5

Dato questo codice:Come posso evitare l'interpolazione della stringa Ruby?

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

La parte #{id} viene interpolato prematuramente.

Come posso evitarlo?

+0

sì perché rubino interpreta come un commento – streetparade

+0

@streetparade: Non è vero. HereDocs non ha commenti ... in effetti, il loro intero purpoase è così che puoi ignorare la formattazione. Tuttavia, si comportano come stringhe tra virgolette (quindi # {code} è eseguito). Il metodo di mckeed dice a Ruby di trattare HereDoc come una stringa con quotatura singola. –

+0

vedere anche http://railsblaster.wordpress.com/2007/08/27/has_many-finder_sql/ –

risposta

12

Mettere apici di tutto il delimitatore:

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

Descritto qui - http://www.cs.auckland.ac.nz/references/ruby/doc_bundle/Manual/man -1.4/syntax.html # here_doc – Gareth