Searching by month

Hi,
Could anybody please help me to search the record by date

I have one products table. In that table I have created_at.I want to
search the records by date.

I mean, if i select month as january and year as 2006 then i want to get
the list as per that month only.
Ex:
Created At Product Price
2006-09-18 Ruby 4.5
2006-09-18 Ruby 4.5
2006-08-18 Ruby 4.5
2006-08-04 shq 8.0

If i select September then I should be able to see

Created At Product Price
2006-09-18 Ruby 4.5
2006-09-18 Ruby 4.5

This is the code i wrote:::
in rhtml file
<%= start_form_tag :action => ‘month’ %>
<%= date_select ‘project’, ‘created_at’,:order=>[:month]%>
<%= submit_tag “Go” %>
<%= end_form_tag %>

In the controller::
def month
[email protected][‘user’][‘id’]
@projects = Project.find_by_sql(“select * from projects where
user_id=’#{userId}’ and month(created_at)= ‘#{[:created_at]}’”)
end

If i give the month as 9 and user id, then i’m getting the result in the
right way. but i want to do it dynamically. so wats the problem with the
code??

Thanks and Regards,
Harish

On 9/20/06, Naga harish Kanegolla [email protected]
wrote:

Created At Product Price

[email protected][‘user’][‘id’]

Have you tried params[:created_at] instead of just [:created_at]?

Cheers
Max

Yes I tried that also. then it gave me Stack level too deep error.

Max M. wrote:

On 9/20/06, Naga harish Kanegolla [email protected]
wrote:

Created At Product Price

[email protected][‘user’][‘id’]

Have you tried params[:created_at] instead of just [:created_at]?

Cheers
Max

Naga harish Kanegolla wrote:

Yes I tried that also. then it gave me Stack level too deep error.

Max M. wrote:

On 9/20/06, Naga harish Kanegolla [email protected]
wrote:

Created At Product Price

[email protected][‘user’][‘id’]

Have you tried params[:created_at] instead of just [:created_at]?

Cheers
Max

hai naga
just tried this

Project.find_by_sql(“select * from projects where
user_id=’#{userId}’ and month(created_at)=
‘#{params[:project][‘created_at(2i)’].to_i}’”)
it may be working

regards
Narayana

Narayana K. wrote:
Hi Narayana
Its giving the same problem. I’m not getting list of records. Its
taking the variable as null.
So I think the problem is with the rhtml file. I think i have to give
the select statement correctly. could u please check this.

<%= start_form_tag :action => ‘month’ %>
<%= date_select ‘project’, ‘created_at’,:order=>[:month]%>
<%= submit_tag “Go” %>
<%= end_form_tag %>

Project.find_by_sql(“select * from projects where
user_id=’#{userId}’ and month(created_at)=
‘#{params[:project][‘created_at(2i)’].to_i}’”)
it may be working

regards
Narayana

Naga harish Kanegolla wrote:

Narayana K. wrote:
Hi Narayana
Its giving the same problem. I’m not getting list of records. Its
taking the variable as null.
So I think the problem is with the rhtml file. I think i have to give
the select statement correctly. could u please check this.

<%= start_form_tag :action => ‘month’ %>
<%= date_select ‘project’, ‘created_at’,:order=>[:month]%>
<%= submit_tag “Go” %>
<%= end_form_tag %>

Project.find_by_sql(“select * from projects where
user_id=’#{userId}’ and month(created_at)=
‘#{params[:project][‘created_at(2i)’].to_i}’”)
it may be working

regards
Narayana
hai naga,

just tried this in rhtml

<%= date_select(:project,:created_at, :order=>[:month]) %>

this having the month value returned in Integer ex: if u selected April
below code give the value 4
params[:project][‘created_at(2i)’].to_i

regards
Narayana

Hi Narayan,
Still its not coming. I have tried puts
params[:project][‘created_at(2i)’].to_i
I have seen in the console. Its giving the value ‘0’. So wat should be
done??

just tried this in rhtml

<%= date_select(:project,:created_at, :order=>[:month]) %>

this having the month value returned in Integer ex: if u selected April
below code give the value 4
params[:project][‘created_at(2i)’].to_i

regards
Narayana

Hi narayana,
It renedered me the correct value only. but wen trying @projects=
Project.find_by_sql(“select * from projects where user_id=’#{userId}’
and month(created_at)= ‘#{params[:project][‘created_at(2i)’].to_i}’”)
I’m getting not getting the list of records. So wat should i do??

Regards,
Harish

I am checking the thing, for me it return the month value correctly.
U just try this
def month
render_text params[:project][‘created_at(2i)’].to_i
end
& see whether it render the month value or not.
& tell me what error was occured in detail

regards,
Narayana

Naga harish Kanegolla wrote:

Hi Narayan,
Still its not coming. I have tried puts
params[:project][‘created_at(2i)’].to_i
I have seen in the console. Its giving the value ‘0’. So wat should be
done??

just tried this in rhtml

<%= date_select(:project,:created_at, :order=>[:month]) %>

this having the month value returned in Integer ex: if u selected April
below code give the value 4
params[:project][‘created_at(2i)’].to_i

regards
Narayana

hai Naga,
I am checking the thing, for me it return the month value correctly.
U just try this
def month
render_text params[:project][‘created_at(2i)’].to_i
end
& see whether it render the month value or not.
& tell me what error was occured in detail

regards,
Narayana

This is a totally different approach than the one you are taking (and
kind of ugly), but you could try…

Project.find(:all, :conditions => {"created_at like ", “’” +
params[:year] + “-” + params[:month] + “%’”})

This seems to work in the console. Not sure it’s production code, but
maybe it will help give ideas.

Good Luck,
Nathan

Naga harish Kanegolla wrote:

Hi narayana,
It renedered me the correct value only. but wen trying @projects=
Project.find_by_sql(“select * from projects where user_id=’#{userId}’
and month(created_at)= ‘#{params[:project][‘created_at(2i)’].to_i}’”)
I’m getting not getting the list of records. So wat should i do??

Regards,
Harish

I am checking the thing, for me it return the month value correctly.
U just try this
def month
render_text params[:project][‘created_at(2i)’].to_i
end
& see whether it render the month value or not.
& tell me what error was occured in detail

regards,
Narayana

hi Harish,

u just tried the below thing in ur db just test the query & look
whether is working or not
for ex:(hard code the value)
select * from projects where user_id= 23
and month(created_at)= 9

regards,
Narayana

Hi Narayan,
It worked me wen i hardcoded also. But thats not working only wen i
use params[:project][’:created_at(2i)’].to_i
So any other idea??

Regards,
Harish

hi Harish,

u just tried the below thing in ur db just test the query & look
whether is working or not
for ex:(hard code the value)
select * from projects where user_id= 23
and month(created_at)= 9

regards,
Narayana

Naga harish Kanegolla wrote:

Hi Narayan,
It worked me wen i hardcoded also. But thats not working only wen i
use params[:project][’:created_at(2i)’].to_i
So any other idea??

Regards,
Harish

hi Harish,

u just tried the below thing in ur db just test the query & look
whether is working or not
for ex:(hard code the value)
select * from projects where user_id= 23
and month(created_at)= 9

regards,
Narayana

Hi Harish
If am not wrong The problem might be some where else bcoz it render the
correct value for params[:project][’:created_at(2i)’].to_i. u just do
one thing just copy the error page & paste here, I will try to fix it.

Regards
Narayana

Naga harish Kanegolla wrote:

Hi Narayana,
Its rendering the correct value and also it gave the list wen i hard
coded. But by dynamically its not coming. Its not giving any error. But
I’m getting the nil records(Empty records).
If I try september month I should get the records of september but it
gave like this…

Created At Project Price

Thats it…

Thanks and Regards,
Harish

Hi Harish
If am not wrong The problem might be some where else bcoz it render the
correct value for params[:project][’:created_at(2i)’].to_i. u just do
one thing just copy the error page & paste here, I will try to fix it.

Regards
Narayana

Hi Harish,
shall we chat in the gmail about the problem
my id: [email protected]

Regards
Narayana

Hi Narayana,
Its rendering the correct value and also it gave the list wen i hard
coded. But by dynamically its not coming. Its not giving any error. But
I’m getting the nil records(Empty records).
If I try september month I should get the records of september but it
gave like this…

Created At Project Price

Thats it…

Thanks and Regards,
Harish

Hi Harish
If am not wrong The problem might be some where else bcoz it render the
correct value for params[:project][’:created_at(2i)’].to_i. u just do
one thing just copy the error page & paste here, I will try to fix it.

Regards
Narayana

Hi Narayana,
Its ok. I added u in Google talk…
Harish

Hi Harish,
shall we chat in the gmail about the problem
my id: [email protected]

Regards
Narayana

Hi Narayana,
Its working good now. I just took colon in the code. Thanku very much
for ur help…
Bye Bye,
Regards,
harish

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs