> I've this entry with a Date attribute, in the yyyy-mm-dd form.
> I need to retrieve all the articles in a data Range, nothing of what I
> read really worked..what's the matter?
> Using find(:all, :conditions .... )
> I've tryed:
> - date between ? and ?
> - date => from..to
> - year(date)
> nothing happened..is ruby able to evaluate range for Dates?
> please show me the way :S
If and only if your date field in the database is a date or a time:
Model.all(:conditions => {:date => 7.days.ago..Date.today})
Model.all(:conditions => ["date BETWEEN ? AND ?",
7.days.ago ,Date.today])
good to know..and thanks for this..but what if a have to choose among
latter periods like from 1993 to 2001?
(I'm dealing with newspaper articles)
And I have in the search form a select with all the available Dates
(in the yyyy-mm-dd) format.
It could be good to me to only use year, so the question is how the
get only the year from a Date type? I've tryed year(date) but it does
not work (al least in the range actually)..or second aswer do I have
to write a method that calculates how many days ago I want? even for
1993 ?
Take the date from your form, which I think you are saying is a string
in "yyyy-mm-dd" format and convert it into a Date object. Then you
can use it in the find conditions as suggested above.
Anyway..this is the only that returns valid results! thanks man!
it works even with 7.years.ago ! cool
so I'd need only to extract the year from the date params I get from
the form..and then maybe calculete how many years ago it is.
year(params[:date]) #?should this work?
>> > I've this entry with a Date attribute, in the yyyy-mm-dd form.
>> > I need to retrieve all the articles in a data Range, nothing of what I
>> > read really worked..what's the matter?
>> > Using find(:all, :conditions .... )
>> > I've tryed:
>> > - date between ? and ?
>> > - date => from..to
>> > - year(date)
>> > nothing happened..is ruby able to evaluate range for Dates?
>> > please show me the way :S
>> If and only if your date field in the database is a date or a time:
>> Model.all(:conditions => {:date => 7.days.ago..Date.today})
>> Model.all(:conditions => ["date BETWEEN ? AND ?",
>> 7.days.ago ,Date.today])
> good to know..and thanks for this..but what if a have to choose among
> latter periods like from 1993 to 2001?
> (I'm dealing with newspaper articles)
> And I have in the search form a select with all the available Dates
> (in the yyyy-mm-dd) format.
> It could be good to me to only use year, so the question is how the
> get only the year from a Date type? I've tryed year(date) but it does
> not work (al least in the range actually)..or second aswer do I have
> to write a method that calculates how many days ago I want? even for
> 1993 ?
Take the date from your form, which I think you are saying is a string
in "yyyy-mm-dd" format and convert it into a Date object. Then you
can use it in the find conditions as suggested above.
it's already a Date it's not a string as I wrote above
If you are using params[:from] then it is a string. params are
always strings. The fact that you keep saying it is in yyyy-mm-dd
format does not make sense if it is a Date as a Date is not in a
particular format till you convert it to string for display for
example.
Build a Date from the string and then you can compare it with your db value.
If you are using params[:from] then it is a string. params are
always strings. The fact that you keep saying it is in yyyy-mm-dd
format does not make sense if it is a Date as a Date is not in a
particular format till you convert it to string for display for
example.
> If you are using params[:from] then it is a string. params are
> always strings. The fact that you keep saying it is in yyyy-mm-dd
> format does not make sense if it is a Date as a Date is not in a
> particular format till you convert it to string for display for
> example.
Check the docs for Date. You might have to use strptime or one of the
other constructor methods. Make sure you catch exceptions if
converting from user typed input.
Occam is always right... the easiest solution if often the best one
(parenthesys work)
morover..
when I use Article.scope(:all, ....
it still behave like an array..
Instead using Article.scoped_by it seems to work better.. but I guess
I can't do something like this:
proxy = Article.scoped_by_author_and_date(aut,"YEAR(date) BETWEEN ?
AND ?",tf.year,tt,year)
@articles = proxy.find_by_solr(params[:query]).results
in the server log I get this:
Article Load (1.2ms) SELECT * FROM `articles` WHERE (articles.id
in (1)) AND (`articles`.`date` = 'date BETWEEN ? AND ?' AND
`articles`.`author` = 'No Author')