I want to use chartkick to create a line graph of records, which belong to a planned task.
In other words: plantask has_many records
Records has two fields I'm interested in graphing. The created_at, which will be my X-axis, and data(An integer), which will be my Y-axis.
So far I've gotten pretty close. By inserting this into my view:
<%= line_chart #plantask.records.group_by_day(:created_at).sum(:data) %>
I can see that my x-axis is displaying perfectly. However, it appears that the y-axis is not loading the records :created_at field, but is loading the :created_at from within the plantask model. (All of my records are mapped to yesterday at 7:00pm) This seems strange to me. Any hints on what I've messed up? Thanks you guys.
It turns out that I was approaching this problem the wrong way. Group by day with sum combines every task into one, and adds the value. What I really needed was this:
<%= line_chart #plantask.records.group(:created_at).sum(:data) %>
Related
I have a date in one of my models, which I am currently rendering in my simple form with
<%= f.input :issue_date, label:false %>
This, I believe automatically applies the date_select helper, causing it to render three very nice selection fields: one for day, one for month one for year.
The problem is that I really can't stand using select for the day and year (just seems more efficient to type it). Is there a good way to change the rendering of these fields? I really like how convenient this automatic rendering is; I'd just like it to render with one select box and two text boxes. Thanks for any suggestions.
I have a 10,000+ row table that I am trying to query in Rails 4. Let's call it events and there are many, many years of past events still in the database. I need to grab only the events with a date of today or in the future. I could do something like:
#events = Event.where('date >= ?', todays_date)
but this still takes a long time as it has to go through every record. Is there a simple way to speed this up? I've even considered that starting from the most recent rows, moving backwards, and limiting to say 1,000 total would be "good enough", although it could potentially leave out an event created a long time ago but with a date in the near future. I'm not even sure how to go about starting from the last row, however. And I feel like there has to be a better solution anyhow.
Any ideas? Thank you in advance!
One thing you can do to speed up the search is to add index to the created_at field (or to the custom date field you are using). Index makes the searching process faster in the database.
You can refer this answer to add index.
It sounds like adding an index to your date column could help with efficiency here.
Migration code below:
class AddIndexToEvents < ActiveRecord::Migration
def change
add_index :events, :date
end
end
I'm a new ruby on rails coder who is trying to create a neatly displayed calendar of the week for students to check their timetables.
I have stored the lessons in a table with the following data:
:course_id, :state_unit_code, :day_of_week, :start_date, :end_date, :start_time, :end_time, :classroom_id, :campus_id, :lecturer_id
I wish to take the data from that table and transform it into an html table similar to that shown:
I've mocked it up using table, tr td rowspan colspan and so on.
If you can point me in the right direction I'd be most appreciable.
Follows on from a post by another user (allesklar): How would you build this daily class schedule?
I would recommend using a gem for this unless you are a strong confident rails programmer with some decent experience. It's a good project if you're just doing it to learn but not so much if you actually want it to be used as a 'production app. in the real world.
I think that https://github.com/elevation/event_calendar might meet your needs. Take a look and see.
You may also need to use a separate gui date picker at some point and their are many solutions for that such as http://code.google.com/p/calendardateselect/ though this is just about picking dates, not the full calendar display of event also. But it could be handy. You'll also see 30 (!) different date pickers here: http://www.1stwebdesigner.com/freebies/jquery-calendar-plugins/ that also include ones that let you span dates.
I am using Sunspot to search for events (parties, concerts, ...) in a Ruby on Rails 3 application.
I have managed to set up free text search and facet search on a couple of different category types.
Now, I am stuck with my next task. I want to set up facets related to when the event is occuring.
I want to have facets describing both relative date/time ranges such as "today", "this weekend", "next weekend" and absolute date/time ranges, such as "Easter Holiday 2011", "New Years Day 2012", ... The datetime ranges are sometimes overlapping each other.
I have browsed around in the Sunspot API documentation, the Sunspot wiki, here at Stackoverflow, read and loads of articles and blogs. People are writing it is possible to to achieve but I find no examples or implementation ideas that makes me understand how to do this.
Any suggestions?
Since my problem is not in my code, I don't publish any code. The class Event has a DateTime instance named "start_time". I do understand it's my job to define when the absolute date/time ranges appear in the calender.
Best regards,
./stefan
PS Did I tell I'm a newbie? ;-) DS
You should set up your fields as trie time fields for efficient range queries:
class Event
searchable do
time :start_time, :trie => true
end
end
Then you can use query facets to facet based on ranges:
Event.search do
facet :start_time do
bod = Time.zone.now.beginning_of_day
row :today do
with :start_time, bod..(bod + 1)
end
row :tomorrow do
with :start_time, (bod + 1)..(bod + 2)
end
# etc.
end
end
The key insight here is that you can construct facets using arbitrary scopes. Note that the facet name :start_time is just used to reference the facet in the search results, and the row labels :today and :tomorrow are similarly just used on the client side to identify the row counts corresponding to those queries; they have no meaning from Solr's standpoint, so you can call them whatever you want (using whatever data type you want -- they don't have to be symbols).
More information on query facets here: http://sunspot.github.com/docs/Sunspot/DSL/FieldQuery.html#facet-instance_method
You can do in this way:
with(:registration_date).between(params[:start_date].to_date..params[:end_date].to_date)
Look here: http://sunspot.github.io/docs/Sunspot.html#search-class_method
I need to have a drop down list in which the user selects the day of the week they want to come in every week. The values will never change right. It's just Sunday, Monday, ..., Saturday right? It seems like more work than needed to make a table and put the days of the week in. I certainly don't need the ability to create, update or delete them. Is there a simple way to handle something like this? Or maybe instead of days of the week it could be status like off, park, reverse, neutral, drive. The main thing is that the values are never going to change. There are just a few of them. So why make a table? I am thinking that there is a way to create a model that already has data in it but I could be wrong.
Why create a model? Just use select.
DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
select(:event, :day, DAYS)
It's usually better practice to place the constant in the relevant model and use it from there.
In your model:
class Event < ActiveRecord::Base
DAYS = ['Monday', 'Tuesday', 'Wednesday', ...]
end
and then in your view:
select(:event, :day, Event::DAYS)
and here's another trick I use a lot:
select(:event, :day, Event::DAYS.collect {|d| [d, Event::DAYS.index(d)]})
Note that Ruby has the English names for the days of the week already built into its date class. You should try to leverage that if you can. Here's the rdoc.
Then as Can has suggested just do the following:
select(:event, :day, Date::DAYNAMES)
Keep in mind that this solution is NOT particularly i18n friendly. If i18n is an issue, I would also check out the localized dates plugin and the changes that were made in Rails 2.2 to support i18n.
Try this:
<%= select_tag(:week_day, options_for_select([['Sunday', 'Sun'], ['Monday', 'Mon'], ...])) %>
See http://guides.rubyonrails.org/form_helpers.html#theselectandoptionstag