Is there rails version of “where column in (value1, value2, …)”?
I know i could do OR many times, but this way is shorter.
I’m ruby/rails newbie, so i have a problem with converting the hash,
which i’m receving from the search form into the string for :conditions
in find method.
Could i instead of creating one, complicated (for me) query do something
like this:
query the database specyfing only one condition (as a condition, i
could use parameter, which is necessary; if it’s not specified there’s
nothing to search for),
then query obtained result set from the previous query with a new
condition (unless it’s nil/empty),
Is there rails version of “where column in (value1, value2, …)”?
I know i could do OR many times, but this way is shorter.
Try this:
values = %w(a b c) @models = Model.find(:all, :conditions => [‘column IN (?)’, values])
I’m ruby/rails newbie, so i have a problem with converting the hash,
which i’m receving from the search form into the string for :conditions
in find method.
This is easy, just unpack the parameters as you go along:
Model.find(:all,
:conditions => [‘expires_at BETWEEN :start AND :end’,
{:start => params[:start], :end => params[:end]}])
Here are some links you might want to investigate:
Thank you (and Ezra Z. and Fabien F.!) very, very much!
This ez_where plugin is really great!
Why it’s not in rails core yet?
BTW.
Would it be possible to create some kind of automatic search plugin
based on ez_where, so that conditions like ==, ===, <=> and so on, where
deterimned by some input hash naming convention or something like this?
I.e. names of form elements would be sth like:
name=“form[is][column]”
name=“form[like][string_column]”
name=“form[lt][numeric_column]” and so on.
Then it would just generate new condition (adding one at a time using
“<<”, if received paremter is not empty), which could later be used
anywhere using “condition.to_sql” method?
Thank you (and Ezra Z. and Fabien F.!) very, very much!
This ez_where plugin is really great!
Why it’s not in rails core yet?
amen
Then it would just generate new condition (adding one at a time using
“<<”, if received paremter is not empty), which could later be used
anywhere using “condition.to_sql” method?
If I understand you correctly, it’s already there…just not in that
form, see the examples on his blog.
Thank you (and Ezra Z. and Fabien F.!) very, very
much!
This ez_where plugin is really great!
Why it’s not in rails core yet?
amen
Thanks guys. I don't really feel like this is core material. It
works perfect as a plugin. I do use it all the time though because I
like the ruby syntax instead of sql. It also needs more polish before
it would be appropriate for inclusion. I think this type of AR
extension is exactly what plugins are great for.
Thanks for using it and please do give feedback or send me a code
snippet if you do something cool with it
name=“form[like][string_column]”
name=“form[lt][numeric_column]” and so on.
Then it would just generate new condition (adding one at a time using
“<<”, if received paremter is not empty), which could later be used
anywhere using “condition.to_sql” method?
If I understand you correctly, it’s already there…just not in that
form, see the examples on his blog.
Craig
Yeah it can already do what you are asking just in a little bit
different syntax. Download the plugin and look at the
ez_where_tests.rb file. That is the best place for now to see all the
things its capable of. There are tons of different ways to use it
so the tests cover all the ways we have discovered so far.
Cheers-
-Ezra
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.