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

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 ok, that is cool, thanks.

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?

Chris Habgood 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,

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

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

wats the code for add combobox in ruby on rails