Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support hash of values #10

Closed
wants to merge 1 commit into from
Closed

support hash of values #10

wants to merge 1 commit into from

Conversation

seanwalbran
Copy link

Add hash_value(s)_of methods and, alternatively, :as_hash option to value(s)_of methods, to address issue #9.

@ernie
Copy link
Owner

ernie commented Feb 11, 2012

I appreciate the pull request and the specs, but I'm not sure I'm sold on the usefulness of the feature itself. Valium's chief asset is its simplicity and lightweight nature. I definitely don't think I like having both hash_value(s)_of and accepting options.

@seanwalbran
Copy link
Author

On the usefulness: I'm dealing with a situation where there's a lot of bulk data manipulation, where the AR overhead is pretty painful, so there's a bunch of this currently in play:

rows_a = ModelA.connection.select_all( raw_sql_here )
rows_b = ModelB.connection.select_all( other_raw_sql_here )
do_stuff_with(rows_a, rows_b)

I'd really prefer to use Valium to leverage existing AR relations instead of raw sql, without the AR instantiation overhead or arel manipulation. Being able to get an array of hashes back provides compatibility with AR DatabaseStatements result set apis (e.g. http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-select_all), and gives value by offering contextual access to the row data instead of having to track array id's independently, which can be fragile.

As far as hash_value(s)_of, or :as_hash options, or even something more 'futureproof' like :as => Hash :as => Array, :as => SomethingElseSomeday, I don't have any particular preference; happy to make this look however makes the most sense.

@seanwalbran
Copy link
Author

As an alternative that wouldn't have so many excess strings flying around, would you be more open to an optional wrapping of the resultset arrays with a lightweight class that overrode [] to accept strings/symbols for accessing the data in addition to numerical indexes?

@matthew342
Copy link

+1 for this feature - have the same sort of situation and am currently mapping the array into a hash.

@steveklebanoff
Copy link

+1 for this feature as well.

@krainboltgreene
Copy link

What we should actually have is the ability to pass in an class and have that class used to instantiate the values:

persons = Person.values_of(:name, :address, :phone, as: Hash)
persons.first["name"] #=> John

The reason I suggest this is because you can also do something like this:

persons = Person.values_of(:name, :address, :phone, as: OpenStruct)

persons.first.name #=> John

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants