I am trying to figure out the best way to display all records by date. I have a date_due column in my database that is a datetime field.
So my output would include every date for which there is an entry, like this:
April 1, 2011
- Buy tickets
- Pickup groceries
April 2, 2011
- Call client
I am trying to use the following method, which is not working: find_all_by_date_due
What's the easiest way to do this in Rails?
Railscasts has a nice cast on group_by :
http://railscasts.com/episodes/29-group-by-month
You could make the dates into an association and assign it an arbitrary ID through rails.
class Entry
belongs_to :date
end
class Date
has_many :entries
end
#entry.date.build(:date => 2011-02-01 20:23:22)
#entry.find_by_date(2)
Date.find(2).date
=> 2011-02-01 20:23:22
Related
I created a new datetime row for a page, it works well. The column displays all the specific dates, but when I want to show specific rows using a date filter, it doesn't display a thing.
In the model:
before_validation :payment_date
def payment_date
self.payment_date = sales_order.payment_transactions.last.date
end
The date filter from the controller file:
filter :payment_date, :as => :date_range, :collection => proc { Complaint.all.map{|c| c.sales_order.try(:payment_transactions).try(:last).try(:date)} }
I suppose I did a couple of mistakes and i'm curious where the bad coding is.
In AA filtering is based on Ransack, so your filter would look as follows (assuming sales_order is an association on your model):
filter :sales_order_payment_transactions_date, label: 'Some label', as: :date_range
Here you basically go along the association chain - it is acceptable with ransack.
I have a Rails app that parses price information from a variety of sources. The models look something like this:
class Price
attr_accessor :value
belongs_to :product
belongs_to :added_pricelist, :class_name => "Scrape"
belongs_to :removed_pricelist, :class_name => "Scrape"
end
class Product
attr_accessor :name
end
class PriceList
attr_accessor :created_at
has_many :prices
end
Every day, a scraper runs, and parses some prices for products from an API. It creates a new PriceList every time the scraper is run. The scraper records which pricelist a price appeared (or disappeared) in - so the data might look like this.
# NB: Assume product_id is always 1 for these
{ value: 100, added_pricelist_id: 1, removed_pricelist_id: 2 }
{ value: 120, added_pricelist_id: 2, removed_pricelist_id: 3 }
{ value: 140, added_pricelist_id: 3, removed_pricelist_id: 4 }
A new object is only created when the price changes, so if it stayed the same, you'd just have:
{ value: 100, added_pricelist_id: 1, removed_pricelist_id: 4 }
My question is: Using ActiveRecord, how can I find the average price of a product for each of the last 30 days? I need to be able to find all the prices that were active on a particular day, and then average that number. Can that be done in a single query? (NB: This is with Postgres).
Feel free to get me to clarify anything that doesn't make any sense, and thanks in advance for helping!
AVG
Although I was a little confused with what you posted, hopefully I can give you some help:
You have at least two options:
ActiveRecord .average
SQL AVG
Both of these perform the same task (except one is ActiveRecord based), so I would look at firstly getting your SQL Query correct, and then performing the AVG function on the data you have
Query
I need to be able to find all the prices that were active on a
particular day, and then average that number
I would do something like this:
#app/models/PriceList.rb
Class PriceList < ActiveRecord::Base
scope :today, -> { where(created_at: DateTime.now.at_beginning_of_day.utc..Time.now.utc).average(:value) }
end
This would allow you to call #product.pricelist.today
Here's my problem, I'll try to be concise.
I have a model Course that has_many Planning.
A Planning has a start_date and an end_date.
I want to retrieve (with sunspot) all the Courses that have a particular planning between a given start_date and an end_date.
The problem is that if I store in the model Course :
searchable do
date :start_date, multiple: true do
plannings.map(&:start_date)
end
date :end_date, multiple: true do
plannings.map(&:end_date)
end
...
end
I loose the fact that start_date and end_date are couples.
Is there a way / trick to store ranges?
Have almost the same problem, in my case start_date and end_date wasn't really a date, just a year (integer), solve it with multiple value fields as range:
searchable do
date :course_date, multiple: true do
plannings.map{|p| (p.start_date..p.end_date).to_a}
end
...
end
so course_date contain all years when courses going. If you have 2001-2003 and 2005-2007(2001, 2002, 2003, 2005, 2006, 2007) and search with equal_to.
A possibility would be to make your query in two times. First find the plannings, then find the courses restricting courses by ID.
Maybe not a very optimised one, but why try to optimize to soon...
Or to index hours with a function:
def happens_between_8_and_9
return true if start_date > 8..
end
searchable do
boolean :happens_between_8_and_9
boolean :happens_between_9_and_10
end
```
I'm working on creating a Rails app that allows users to set availability Sunday through Saturday, with a start time, end time, and location (simple string). I have an User object and I'm unsure of how to continue. I know I could have a large set of Time objects like so:
class User
field :mondayStartTime, :type => Time
field :mondayEndTime, :type => Time
field :mondayLocation, :type => String
field :tuesdayStartTime, :type => Time
field :tuesdayEndTime, :type => Time
field :tuesdayLocation, :type => String
...
field :sundayEndTime, :type => Time
field :sundayLocation, :type => String
end
However, this seems terribly inefficient, though creating another unique object class seems just as bad and only results in more lines of code. Is there an easier way of implementing start/end times for a Sunday through Saturday schedule?
Thanks!
At first glance, I'd create an "Availability" model/class (or something less prone to misspelling like "Schedule") with the attributes of start_at, end_at, location, and day_of_week - and have that class belong to the User (and with the User having a "has_many :availabilities" or "has_many :schedules"). That seems (again at first glance) to be the most efficient and easiest to get the data back out - and you can use that separate class to do things like grouping schedules/availabilities together more easily.
Have you thought about creating objects for each day of the week, that have a start, end and location property?
Or perhaps, a Jobs object(model) that has fields for start time, end time, and location.
This way it would be possible to do, has_many :jobs and then you can either narrow down, by day, or select by a particular day. The narrowing down should be feasible with the ruby Time Class or ActiveSupport class (3.days.from_now, etc).
irb(main):001:0> require 'active_support/all'
=> true
irb(main):002:0> 2.days.ago
=> 2012-04-03 08:30:31 -0700
irb(main):003:0> 2.days.from_now
=> 2012-04-07 08:33:01 -0700
EDITED TO SHOW ACTIVE SUPPORT TIME CLASS
I may have over-complicated what I need to do but this is what I now have.
I have a jobs controller that has 2 fields
starts_at as DATETIME
end_time as DATETIME
I save the event_date in a form using a
calendar_date_select :starts_at ,:time => false
I save the date time as
time_select :starts_at, {:twelve_hour => true}
This saves the Event date i.e 12/26/2009 and the start time as 7:00 pm
I need to also save the event end_time without having to re-enter the date, just the time
time_select :end_time, {:twelve_hour => true}
If i just use the above, the time is correct but the date dafaults to 01/01/2000
How do I save the date with the same date from starts_at?
In your controller, if the :end_time is nil, set it to the :starts_at variable like so:
object.end_time = object.starts_at unless not object.end_time.nil?
You may be able to shorten the end to
object.end_time = object.starts_at unless object.end_time.present?
but I'm not sure which version of Rails you are using and what the default implementation of .present? does for dates.
You could do this in the model or controller, but isn't it possible that an event will start late at night and end on the following day? To allow for this possibility I'd recommend having two calendar_date_selects and auto-assigning the ending date when the starting date is chosen (so the second calendar_date_select will rarely be used).
If you really don't want two calendars, maybe use a hidden field for the ending date that follows the start date.
I wouldn't enforce this same-date rule below the interface level unless you're absolutely sure different start and end dates will never occur.