Displaying 3 results from an estimated 3 matches for "index_by".
Did you mean:
index_0
2006 Jul 25
9
Can anyone explain this code? It uses Inject
def webs
@webs ||= Web.find(:all).inject({}) { |webs, web|
webs.merge(web.address => web) }
end
--
Posted via http://www.ruby-forum.com/.
2006 Jul 02
0
Rails Core Weekly June 19 - July 2 2006
...payments.sum(&:price)
or simply block-less:
[1,2,3].sum # => 6
DHH explains this is instead of
payments.inject(0) { |sum, p| sum + p.price }
In fact the implementation of #sum is virtually the same:
inject(0) { |sum, element| sum + yield(element) }
Quickly thereafter Nick Seckar adds index_by to Enumerable which
turns an Enumerable into a hash like so:
people.index_by(&:login)
So this hash
{5 => payments[0], 15 => payments[1], 10 => payments[2]}
is pretty much equal to
payments.index_by(&:price)
but certainly a lot prettier.
Then of course Active Resource is adde...
2012 Nov 03
0
ids writer fields for HABTM relationship.
...:
[code]
# Implements the ids writer method, e.g. foo.item_ids= for
Foo.has_many :items
def ids_writer(ids)
pk_column = reflection.primary_key_column
ids = Array.wrap(ids).reject { |id| id.blank? }
ids.map! { |i| pk_column.type_cast(i) }
replace(klass.find(ids).index_by { |r| r.id }.values_at(*ids))
end
[/code]
This method gets called by the People controller in the update action
(@person.update_attributes params[:person]), which in turn deeper in
Rail''s guts arrive to the pinpointed method when accessing an *_ids
writer...
What is very weird, is th...