Rails code to sql

Is there a relatively easy way to do this code in sql?

@categories=Category.all.reject{|n| n.blank?}.sort{|a,b| a.name
<=>b.name}.collect{|t| [t.name,t.name.downcase.gsub(’ ‘,’_’)]}

This filters out rows where name is null and orders by name in SQL:
Category.where(“name NOT NULL”).order(“name ASC”)

I’m not sure how to get mysql to downcase the column, but I’m pretty
sure there’s a way.

Luke

Luke C. wrote in post #969470:

This filters out rows where name is null and orders by name in SQL:
Category.where(“name NOT NULL”).order(“name ASC”)

I’m not sure how to get mysql to downcase the column, but I’m pretty
sure there’s a way.

I believe the standard SQL function for that is lower(). Check the
docs.

Luke

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

It is, how do I implement it with the Rails 3 finders?

Hi Chris, you could try something like this:
categories = Category.select("*, LOWER(name) as lower_name")

The calculated column ‘lower_name’ would now be available as a accessor
on each result. eg. categories.first.lower_name

Luke

OH, is there a way to do the REMOVE sql command using the finders so I
would
not have to loop over the whole set and replace items?

Oh ok, that is cool, thanks.

I know enough sql I was not sure where to put it in the rails 3 finders.

fyi, it goes in the select portion.

Chris H. wrote in post #969587:

OH, is there a way to do the REMOVE sql command using the finders so I
would
not have to loop over the whole set and replace items?

Yes. You can write your WHERE clause to do that.

Now, put aside Rails for the next few days, and study a good elementary
SQL reference. If you’re asking questions like these, you need to go
learn SQL as such.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Sent from my iPhone

On Monday 20 December 2010, Chris H. wrote:

fyi, it goes in the select portion.

No, you’re probably doing something wrong here. Look at the docs for
#delete_all. Also, if you look at the code (“show source”), you’ll
notice that you don’t have to pass conditions to #delete_all directly.
Alternatively, you can use #where, i.e., MyClass.where(…).delete_all.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

wats the code for add combobox in ruby on rails