Oh I see what you mean. You’re saying that the docs example show this:

Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", 
author_id, start_date]
Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR 
approved_by = :user_id", { :user_id => user_id }]


But again, knowledge of Ruby tells me that the is no programatic difference 
between calling a method with an array and passing arguments to an array. 
(Unless this has changed in Ruby, I’m pretty sure ruby basically treats these 
as the very same thing)

I see that in other parts of the Ruby documentation, the splat is shown in the 
method title (which is really just an arbitrary thing for us the developers 
since you never actually write this)

As in here: http://api.rubyonrails.org/v2.3.8/classes/ActiveRecord/Base.html 
<http://api.rubyonrails.org/v2.3.8/classes/ActiveRecord/Base.html>

attr_accessible(*attributes)


So in this case I would say that the correct documentation should be:

find_by_sql(sql, *binds = [])



> On Jun 25, 2015, at 10:22 AM, James Coleman <jtc...@gmail.com> wrote:
> 
> >> 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)
> 
> And yet that's not what the document examples do...
> 
> Does the method doc not show the splat operator? Because in the quoted method 
> doc, the * isn't shown, which would lead to confusion even if that's what 
> being used under the hood.
> 
> On Thu, Jun 25, 2015 at 10:18 AM, Jason Fleetwood-Boldt <t...@datatravels.com 
> <mailto:t...@datatravels.com>> wrote:
> 
> 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 
>> <mailto: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 
> <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 
> <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