Rails 3 Impressionist indexing by day - ruby-on-rails

I am using the Ruby gem Impressionist. It gives you the ability to look at a count of page hits from a specific date to today date. I'm trying to get the hits from the past 7 days but spit out the number for each day.
#widget.impressionist_count(:start_date=>"2011-01-01") #specify start date only, end date = now
How would I do that? I want 7 days ago but for each day to give me the exact count for that day.

Try this:
#widget.impressionist_count(:start_date => 1.week.ago)

You can get 1 week ago date like this
t = Time.now
lastweek = t - 1.week
and then you can do like this
#widget.impressionist_count(:start_date=>lastweek.strftime('%Y-%m-%d'))
I guess you shold do this in controller, or you can pass just the last week date to the view.
This code works in Rails app cause it uses active_support.
I tried impressionist with mongoid but the filter function didn't work at all in my enviroment.
i hope it does in your app.

Related

.change function is not working for Dates for even number of months in ruby

Hi I have define this method
def change_date
date = Date.today
start_date = date.change(year: 2015, month: (2 * 3)).at_beginning_of_quarter
p 'aaaaaa'
p start_date
end
give me invalid date error .change is not working or am I doing it in a wrong way please guide me how to solve this. Thanx in advance.
This is because the month you are specifying doesn't have the current day.
I mean the current month (July) has 31 days but the month you're setting (June) has only 30 days. You can change your code like so:
# in Rails:
date = Date.today.beginning_of_month # or Date.today.change(day: 1)
Then chain your 'change' in front of the date variable.
This actually happens, because today is the 31 of July, and not all months have 31 days in it, for example June, the 6th month, has only 30 days in it.

Day and Time fields using Ruby on Rails

I am new to Ruby and Rails . I have a requirement in which I have to store just the day of the week and time in the database.
Can I get the gudiance on how to implement the above requirement.
Absent more usage details, I'd keep it simple.
Store the day of the week as an integer where 0 = Sunday.
Store the time as a string where "13:45" = 1:45pm.

Iterating the dates in ruby on rails

I am developing an application for reporting and analytic,where I need to generate data on a daily basis and put it in a CSV.To be more clear, if I take the report today like day one any how I can get the report thats already done, if I get the report tomorrow I should be able to get the data of today,yesterday. If i take after 4 days I should get the report starting from day 1 to day 4.
How it can be done. Looking for heads up
How about this:
today = Date.today
tomorrow = today.next
four_days_ago = 4.days.ago(today)
four_days_later = 4.days.since(today)
(today..(4.days.since(today)).each {|d| puts d}

Find all records that were created on a specific day of the week

I need to find all records that were created on a specific day of week.
I only have available to me the standard model datetime timestamps.
How would I go about doing this in activerecord?
To follow up on Justin's answer
where("extract(dow from created_at) = ?", Date.today.wday)
This is what I'm using in my application for postgres. This will find all records that were created on the same day-of-week as today. For example, if today was tuesday it would find all records created on tuesdays.
You can use the DAYOFWEEK function in MySQL and pass it to the :conditions option. Supposing you have a model called Item, this would return all of the items created on Sunday:
Item.all(:conditions => ['dayofweek(created_at) = ?', 1])
Using Postgres you could do something similar with to_char.
Note that using a function like this will probably make the database do a full table scan, since at least MySQL doesn't support adding an index to a function. You may want to consider extracting the day of week out to another column if this is something that you anticipate doing frequently.
Obtain the seconds since Unix Epoch. Time.to_i does this in Ruby.
Use modulus of 7 to obtain the day of the week (0 to 6).
dayOfWeek = (epochseconds / 86400 ) % 7;
If you're not opposed to using ruby you could try this.
array.select { |arr| ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"].include?(arr.created_at.strftime('%A'))
I originally tried using dayofweek that was suggested in another answer.
The issue I ran in to was that it seems like my sql server was using UTC time and my rails server was using Eastern US. Records created after 8pm would be picked up while those that happened before would be considered the previous day.
Here is another related question:
How to filter by day of week in Rails 4.2 and sqlite?

Rails: find by day of week with timestamp

I need to grab the records for same day of the week for the preceeding X days of the week. There must be a better way to do it than this:
Transaction.find_by_sql "select * from transactions where EXTRACT(DOW from date) = 1 and organisation_id = 4 order by date desc limit 7"
It gets me what I need but is Postgres specific and not very "Rails-y". Date is a timestamp.
Anyone got suggestions?
How many days do you want to go back?
I have written a gem called by_star that has a dynamic finder suited for finding up to a certain number of days in the past. If the number of days was always a number you could use this finder:
Transaction.as_of_3_days_ago
If it was dynamic then I would recommend using something such as future or between, depending on if you have transactions in the future (i.e. time travel):
Transaction.future(params[:start_date].to_time)
Transaction.between(params[:start_date].to_time, Time.now)
AFAIK Rails has no any methods to do this by other way. So best, and faster, solution - build DOW index on date column and use your query.

Resources