Ruby sql question

Hi,

Im using ActiveRecord to find something.

<% @new = User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN
(array)") %>

Now, i can't seem to make it look through the array. I had a look for
this problem in other languages and in php its simply $array.

···

--
Posted via http://www.ruby-forum.com/.

Perhaps:
"SELECT * FROM users WHERE users.id NOT IN (#{array.join(', ')})"

···

On Wed, Oct 14, 2009 at 4:24 PM, Manish Sharma <manish16s@gmail.com> wrote:

Hi,

Im using ActiveRecord to find something.

<% @new = User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN
(array)") %>

Now, i can't seem to make it look through the array. I had a look for
this problem in other languages and in php its simply $array.

With ActiveRecord you can use:

     User.all(:conditions => ["users.id NOT IN (?)", array])

···

On Wed, 14 Oct 2009, Manish Sharma wrote:

Hi,

Im using ActiveRecord to find something.

<% @new = User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN
(array)") %>

Leslie Viljoen wrote:

···

On Wed, Oct 14, 2009 at 4:24 PM, Manish Sharma <manish16s@gmail.com> > wrote:

Hi,

Im using ActiveRecord to find something.

<% @new = User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN
(array)") %>

Now, i can't seem to make it look through the array. I had a look for
this problem in other languages and in php its simply $array.

Perhaps:
"SELECT * FROM users WHERE users.id NOT IN (#{array.join(', ')})"

Thank you Leslie. That worked fine.
--
Posted via http://www.ruby-forum.com/\.

Leslie Viljoen wrote:

···

On Wed, Oct 14, 2009 at 4:24 PM, Manish Sharma <manish16s@gmail.com> > wrote:

Hi,

Im using ActiveRecord to find something.

<% @new = User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN
(array)") %>

Now, i can't seem to make it look through the array. I had a look for
this problem in other languages and in php its simply $array.

Perhaps:
"SELECT * FROM users WHERE users.id NOT IN (#{array.join(', ')})"

Just an extra question though. what does the array.join(',') do?
--
Posted via http://www.ruby-forum.com/\.

Matt Haley wrote:

Hi,

Im using ActiveRecord to find something.

<% @new = User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN
(array)") %>

With ActiveRecord you can use:

    User.all(:conditions => ["users.id NOT IN (?)", array])

Sweet, just learning ActiveRecords, but was using .find( :all ... )
I like this!

···

On Wed, 14 Oct 2009, Manish Sharma wrote:

--
Kind Regards,
Rajinder Yadav

http://DevMentor.org
Do Good ~ Share Freely

http://www.ruby-doc.org/core/classes/Array.html#M002182

Returns a string created by converting
each<class Array - RDoc Documentation
of the array to a string, separated by
*sep*.

   [ "a", "b", "c" ].join #=> "abc"
   [ "a", "b", "c" ].join("-") #=> "a-b-c"

Also, your use of erb and ActiveRecord implies a MVC structure. If this is
the case, you should be assigning instance variables in your controller and
not your view. You should not have find statements or assignments in your
view, it should only be for formatting output.

···

On Wed, Oct 14, 2009 at 9:47 AM, Manish Sharma <manish16s@gmail.com> wrote:

Leslie Viljoen wrote:
> On Wed, Oct 14, 2009 at 4:24 PM, Manish Sharma <manish16s@gmail.com> > > wrote:
>> Hi,
>>
>> Im using ActiveRecord to find something.
>>
>> <% @new = User.find_by_sql("SELECT * FROM users WHERE users.id NOT IN
>> (array)") %>
>>
>> Now, i can't seem to make it look through the array. I had a look for
>> this problem in other languages and in php its simply $array.
>
> Perhaps:
> "SELECT * FROM users WHERE users.id NOT IN (#{array.join(', ')})"

Just an extra question though. what does the array.join(',') do?
--
Posted via http://www.ruby-forum.com/\.

Just play around with it in IRB:

irb(main):002:0> ['a', 'b', 'c'].join(',')
=> "a,b,c"

I think that when PHP converts $array to a string, it puts commas in
between each element, which is just lucky for the purposes of
producing a list that the SQL statement would like.

This is what happens when Ruby converts an array to a string:
irb(main):003:0> ['a', 'b', 'c'].to_s
=> "abc"

So to put in the commas like PHP does it (and SQL likes it), use join.