You are incorrect the documentation is accurate.

You need to have a deeper understanding of ruby, and specifically the magic of 
the splat (*) operator. 

You do not need to pass all the arguments inside of an array, the correct 
syntax is:

Post.find_by_sql("SELECT * FROM posts WHERE id = ?", 1)

‘binds’ is just what you think it is, the variables assigned to the 
replacements noted by the ? characters. If you had two, it would look like so:


Post.find_by_sql("SELECT * FROM posts WHERE id = ? AND foo != ?", 1, 2)

This works because of how the splat operator works under the hood, taking the 
arity past the first argument and turning it into an array on the receiving 
side of the called method (find_by_sql)

You should have a deeper understanding of the advanced parts of Ruby (like 
splats) before moving into Rails.

The documentation is standard according to how Ruby is documented. 

-Jason



> On Jun 25, 2015, at 8:38 AM, Matias Korhonen <korhonen.m...@gmail.com> wrote:
> 
> Hi,
> 
> 
> We happened to need #find_by_sql today and noticed that it appears to have a 
> somewhat confusing interface/method definition/documentation.
> 
> The method definition is:
> 
> def find_by_sql(sql, binds = [])
>   # ...
> end
> 
> Which would seem to imply that you should use it like this:
> 
> Post.find_by_sql("SELECT * FROM posts WHERE id = ?", [1])
> 
> However, that is wrong, you actually need to do (and the examples in the 
> documentation are like this):
> 
> Post.find_by_sql(["SELECT * FROM posts WHERE id = ?", 1])
> 
> I spent some time reading the documentation and code and can't figure out 
> what the binds argument should be used for.
> 
> Maybe some sort of example or explanation should be added to the #find_by_sql 
> documentation? I'd do so myself, but I'm having trouble figuring it out…
> 
> 
> — Matias
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-core+unsubscr...@googlegroups.com 
> <mailto:rubyonrails-core+unsubscr...@googlegroups.com>.
> To post to this group, send email to rubyonrails-core@googlegroups.com 
> <mailto:rubyonrails-core@googlegroups.com>.
> Visit this group at http://groups.google.com/group/rubyonrails-core 
> <http://groups.google.com/group/rubyonrails-core>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to