@Jason, I don't think you're correct about the way Ruby functions. Splats
are not implicit; they must be declared in the method definition. For
example:

irb(main):001:0> def t(x, y=[])
irb(main):002:1> puts x
irb(main):003:1> puts y.inspect
irb(main):004:1> end
=> :t
irb(main):005:0> t(1, 2)
1
2
=> nil
irb(main):006:0> t(1,2,3)
ArgumentError: wrong number of arguments (3 for 1..2)
from (irb):1:in `t'
from (irb):6

So clearly if you call a method like that with two arguments, the second
will not be coerced into an array like it would if you used splat, and
further the method call fails if you pass more than 2 arguments. Hence
splat is not implicit.

And the method definition for find_by_sql does not define a splat
(see rails/activerecord/lib/active_record/querying.rb) so it cannot
possibly function the way you claim it does. Have you tested it out?

I have tested it out, and it fails with more than two arguments to the
method call as one would expect based on the method signature:

irb(main):002:0> Client.find_by_sql('select ?, ?', 1, 2)
ArgumentError: wrong number of arguments (3 for 1..2)
from
/Users/.../.rbenv/versions/ruby-2.1/lib/ruby/gems/2.1.0/gems/activerecord-4.2.2/lib/active_record/querying.rb:38:in
`find_by_sql'
from (irb):2

On Thu, Jun 25, 2015 at 10:41 AM, Matias Korhonen <korhonen.m...@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)
>
>
> I wonder if you actually tried that in console? Because I did, with this
> result:
>
> [1] pry(main)> Post.find_by_sql("SELECT * FROM posts WHERE id = ?", 1)
> *NoMethodError: undefined method `empty?' for 1:Fixnum*
> from
> /Users/matt/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.10/lib/active_record/connection_adapters/abstract_adapter.rb:389:in
> `without_prepared_statement?'
>
>
> You should have a deeper understanding of the advanced parts of Ruby (like
>> splats) before moving into Rails.
>
>
> Having used Ruby professionally for the past five years or so, I have
> pretty good understanding of basic Ruby behaviour (like splats), so your
> snark is unwarranted (and not a good way to make people feel wanted in this
> community).
>
>
> So in this case I would say that the correct documentation should be:
>> *find_by_sql*(sql, *binds = [])
>
>
> Except that's not what the method definition *actually* is. There is no
> splat in the definition
> <https://github.com/rails/rails/blob/70d1b5a7f8e25b077168deaf592e0e58c3f2bdd1/activerecord/lib/active_record/querying.rb#L38-L52>.
> If there was, the behaviour would be different (if it wasn't for the fact
> that def find_by_sql(sql, *binds=[]) would be invalid ruby
> <https://gist.github.com/matiaskorhonen/3e79c6bcf2df8fbc7eec>).
>
>
>
>  --
> 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.
>

-- 
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