D
Drew Olson
I have an Event model which has_many :attendees. My Attendee model has a
column called participants, which is an integer representing the number
of participants in the attendees party. I want to sum the total number
of participants for a given event. This works fine if I just the default
option of 0 for the method inject, but inject fails if you don't give it
a default. In ruby, injects normal behavior will default the sum to 0 if
you don't provide an argument. See my console output below:
=> #<Event:0x34d95ec @attributes={"title"=>"Canned Food Drive",
"date"=>"2007-02-01 12:00:00", "id"=>"1", "description"=>"We need a
least 20 people to sign up for the canned food drive. This will could
toward service hours and will be a great event to help younger scouts
get involved with the community. We will be collecting, sorting and
delivering donated cans.", "rsvp_date"=>"2007-01-15",
"permission_url"=>"/docs/permission.jpg", "location"=>"Saint David's"}>=> #<Attendee:0x34cc7ac @attributes={"name"=>"Drew Olson",
"event_id"=>"1", "participants"=>"1", "contact"=>"847.942.2606",
"id"=>"1"}>
As you can see, if a default of 0 is not provided, inject returns an
instance of the Attendee class rather than the value in sum. Very
strange...
column called participants, which is an integer representing the number
of participants in the attendees party. I want to sum the total number
of participants for a given event. This works fine if I just the default
option of 0 for the method inject, but inject fails if you don't give it
a default. In ruby, injects normal behavior will default the sum to 0 if
you don't provide an argument. See my console output below:
=> #<Event:0x34d95ec @attributes={"title"=>"Canned Food Drive",
"date"=>"2007-02-01 12:00:00", "id"=>"1", "description"=>"We need a
least 20 people to sign up for the canned food drive. This will could
toward service hours and will be a great event to help younger scouts
get involved with the community. We will be collecting, sorting and
delivering donated cans.", "rsvp_date"=>"2007-01-15",
"permission_url"=>"/docs/permission.jpg", "location"=>"Saint David's"}>=> #<Attendee:0x34cc7ac @attributes={"name"=>"Drew Olson",
"event_id"=>"1", "participants"=>"1", "contact"=>"847.942.2606",
"id"=>"1"}>
As you can see, if a default of 0 is not provided, inject returns an
instance of the Attendee class rather than the value in sum. Very
strange...