Noob question on active record

Hi all,

Sorry if this is too obvious for most, but I am new to rails and ruby
in general, so basically at lost at what I can do. Here is the set

I’ve set up a parent and child relation (ie. parent “has many”
child). The child object has these attributes (id, name, day,
period…), where day is a fix set of string (monday, tuesday, …
sunday) and period is all a fix set of string like (morning, noon,
night). I am using the this plugin for enum attributes for day and

What I want to do in rails is print out all the child of a parent in
the following formatted table.


morning | noon | night
id | id | id
name | name | name


morning | noon | night
id | id | id
name | name | name


morning | noon | night
id | id | id
name | name | name

What I did was to create a method in parent to get all the child with
the associated parent_id, and tried to create a hash with the key
being “day” and the values being the record. So the idea is to have
hash value like this.

hash[“monday”] => { {morning, 1, “test1”}, {noon, 2, “test2”}, {night,
3, “test3”}}

With that I “think” I can loop through this hash in view (don’t know
how to do that yet).

I can’t seem to create the hash result by now, can anyone help? Or is
there any easier way to do this?

The function that I have hash is this (which doesn’t work):

def find_child_by_day
hash = {}
result = Deal.where("parent_id = :pid ",
{:vid => 1})
result.each do |r|
hash[] ||= []
hash[] << r

Hej Herman,

did you take a look at the group method.

I bet you can do it easier.


Hi Sebastian,

I did try group on activerecord, what I did was group by “day”.
However, after that, how do I print out the table I layout before?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs