Undefined Method 'Group_By_Day' - Rails 3.2

undefined method `group_by_day' - rails 3.2

To have group_by_day supported, you need to bundle another gem: groupdate from the same author of Chartkick.

undefined method `group_by_day' for Groupdate + Chartstack graph

If you look at the documentation for groupdate,
the group_by_day method works on the collection of objects, not the specific attribute.

The following code works on the array of orders:

<%=  
line_chart @orders.group_by_day(default_value: "missing", time_zone: "Kolkata") { |order|
order.created_at
}.map { |date, orders| [date, orders.size] }
%>

From the docs on groupdate, group_by_day is a method that works on a collection of objects, either Enumerable or ActiveRecord collections. When called on an Enumerable object like an array, it uses the return value of the block to know which attribute of the object to group by, in this case your created_at attribute. The last method call to map returns data that chartkick can use to render the line graph, in this case chartkick expects an array of arrays which contain the date and the count of items per date (or some aggregated value).

How do I group by day instead of date?

Time is a quite complex object to group by. Assuming you want to group by the creation date, instead of the full Time, start creating a custom method in your model to return the group criteria.

The method should return the creation date, possibly as string.

def group_by_criteria
created_at.to_date.to_s(:db)
end

Then, group by that method.

list.group_by(&:group_by_criteria).map {|k,v| [k, v.length]}.sort

Looping Multiple Arrays by year

I think you can try this way. in this you have not need to check for blank values. i hope @a contains all your available years otherwise you needs to merge all years an loop through years

<table>
<% @a.each do |a| %>
<tr>
<td><%= a.year %></td>
<td><%= (a.id || 0 rescue 0) %></td>
<td><%= (a.data || 0 rescue 0) %></td>
<td><%= (@b.select{|h| h if h["year"] == a.year }.first.id || 0 rescue 0) %></td>
<td><%= (@b.select{|h| h if h["year"] == a.year }.first.data || 0 rescue 0 ) %></td>
<td><%= (@c.select{|h| h if h["year"] == a.year }.first.id || 0 rescue 0) %></td>
<td><%= (@c.select{|h| h if h["year"] == a.year }.first.data || 0 rescue 0) %></td>
<td><%= (@d.select{|h| h if h["year"] == a.year }.first.id || 0 rescue 0) %></td>
<td><%= (@d.select{|h| h if h["year"] == a.year }.first.data || 0 rescue 0) %></td>
</tr>
<% end %>
</table>

If your @a not contains all years then just try to merge it by

years = ((@a.collect{|h| h["year"]}) + (@b.collect{|h| h["year"]}) + (@c.collect{|h| h["year"]}) + (@d.collect{|h| h["year"]})).uniq

then you can just looping through years array as:

<% years.each do |year| %>
<tr>
<td><%= year %></td>
<td><%= (@a.select{|h| h if h["year"] == year }.first.id || 0 rescue 0) %></td>
<td><%= (@a.select{|h| h if h["year"] == year }.first.data || 0 rescue 0 ) %></td>
<td><%= (@b.select{|h| h if h["year"] == year }.first.id || 0 rescue 0) %></td>
<td><%= (@b.select{|h| h if h["year"] == year }.first.data || 0 rescue 0 ) %></td>
<td><%= (@c.select{|h| h if h["year"] == year }.first.id || 0 rescue 0) %></td>
<td><%= (@c.select{|h| h if h["year"] == year }.first.data || 0 rescue 0) %></td>
<td><%= (@d.select{|h| h if h["year"] == year }.first.id || 0 rescue 0) %></td>
<td><%= (@d.select{|h| h if h["year"] == year }.first.data || 0 rescue 0) %></td>
</tr>
<% end %>
</table>

Rails Gem for twitter follow like functions between users

There are a couple gem solutions out there you might want to take a look at:

Acts as Follower

https://github.com/tcocca/acts_as_follower

Amistad

https://github.com/raw1z/amistad

Whether or not you write from scratch is really a question of how much control you want over the final product. If you or someone you're working with/for wants the ability to change many simple details or one or more concepts, you'll want to write it from scratch. If you're willing to just take what comes to trade off for development speed, gems are a great solution.

Welcome to Rails and StackOverflow!



Related Topics



Leave a reply



Submit