What does & in front of symbol do for a map iterator?

Hey all,

In this line:

visits.map(&:visit_status_id).max == VisitState.get(state).id

I’m just curious what that ampersand is doing there and its relevance to
being passed into map with the symbol.

Also, when you specify a has_many :visits, for example, will that create
a local variable in the model called visits, which contains the
collection of records from the visits table?

Thanks for response

  1. That’s the “Symbol#to_proc” shortcut:

http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html

  1. A has_many declaration will create methods for you (not a local
    variable.)

http://guides.rubyonrails.org/association_basics.html#has_many-association-reference

  1. A has_many declaration will create methods for you (not a local
    variable.)

http://guides.rubyonrails.org/association_basics.html#has_many-association-reference

thanks for response

But if visits is a method, how can you call map on it? Map can only be
called on enumerables like array, string, and hash, right? Or does the
visits method return an array of visit records and if the returned value
is an array, then map is called on that array

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