Ruby sql question

M

Manish Sharma

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

Leslie Viljoen

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(', ')})"
 
M

Manish Sharma

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

Just an extra question though. what does the array.join(',') do?
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Just an extra question though. what does the array.join(',') do?
http://www.ruby-doc.org/core/classes/Array.html#M002182

Returns a string created by converting
each<http://www.ruby-doc.org/core/classes/Array.html#M002173>element
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.
 
L

Leslie Viljoen

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

Matt Haley

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])
 
R

Rajinder Yadav

Matt said:
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!


--
Kind Regards,
Rajinder Yadav

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,446
Latest member
Pycoder

Latest Threads

Top