ruby - Rails 4 + ActiveRecord: group_by on column from different model -
i have these models:
class user < activerecord::base has_many :car_votes has_many :cars end class car < activerecord::base belongs_to :user has_many :car_votess end class gamevote < activerecord::base belongs_to :user belongs_to :car end
if want display cars user voted (and group output day when user voted cars):
@voted_cars = @user.car_votes.order('car_votes.created_at desc').group_by { |r| r.created_at.to_date }
so on output cars user voted grouped days user voted games.
but group output according car_votes.created_at
, according cars.created_at
. how this?
thank in advance.
if associations allow this(try on rails console):
@c = carvote.first puts @c.car.created_at
and return value rather error, can do:
@voted_cars = @user.car_votes.order('car_votes.created_at desc').group_by { |r| r.car.created_at.to_date }
Comments
Post a Comment