On Tuesday, April 7, 2015 at 3:52:47 PM UTC-7, Steven Ringo wrote:
>
> Hi,
>
> I am having issues with to_hash_groups, specifically when invoking it by
> providing an array of column names for the key_column and nothing for the
> value column.
>
> As per the documentation
> <http://sequel.jeremyevans.net/rdoc/classes/Sequel/Dataset.html#method-i-to_hash_groups>
> :
>
> DB[:table].to_hash_groups([:first, :middle])
>
> My example:
>
> DROP TABLE IF EXISTS "people";
>
> CREATE TABLE "people" (
> id SERIAL PRIMARY KEY,
> first_name varchar(255) default NULL,
> last_name varchar(255) default NULL,
> state varchar(50) default NULL,
> country varchar(100) default NULL
> );
>
> INSERT INTO
> "people" (first_name,last_name,state,country)
> VALUES
> ('Kasimir','Simmons','WI','United States'),
> ('Vivien','Avila','LA','United States'),
> ('Brett','Marsh','FL','United States'),
> ('Florence','Blair','WA','Australia');
>
> I am using Sequel::Model in a rails application
>
> When invoking Person.to_hash_groups([:state, :country]), it raises the
> following error:
>
> NoMethodError: undefined methodvalues_at' for #Person:0x007f94de3557a8`
>
> using #naked on the recordset works around the problem as an interim
> solution:
>
> Person.naked.to_hash_groups([:state, :country])
>
> The conditional at lib/sequel/dataset/actions.rb#L732
> <https://github.com/jeremyevans/sequel/blob/9cc1ba7ecc682fe2c5af4c876356806f83ce0b02/lib/sequel/dataset/actions.rb#L732>
> that's
> not being called for this example might have something to do with the
> problem.
>
> Any help would be appreciated.
>
Thanks for submitting this with some more detail. I think I've found the
problem, it only happens for cases where you call to_hash_groups or to_hash
with a single array on a dataset that uses a row proc (which model datasets
do). Here's a patch, I'll try to commit this tomorrow after some more
testing:
diff --git a/lib/sequel/dataset/actions.rb b/lib/sequel/dataset/actions.rb
index 08c43f5..c67f8f6 100644
--- a/lib/sequel/dataset/actions.rb
+++ b/lib/sequel/dataset/actions.rb
@@ -701,7 +701,7 @@ module Sequel
end
end
elsif key_column.is_a?(Array)
- each{|r| h[r.values_at(*key_column)] = r}
+ each{|r| h[key_column.map{|k| r[k]}] = r}
else
each{|r| h[r[key_column]] = r}
end
@@ -744,7 +744,7 @@ module Sequel
end
end
elsif key_column.is_a?(Array)
- each{|r| (h[r.values_at(*key_column)] ||= []) << r}
+ each{|r| (h[key_column.map{|k| r[k]}] ||= []) << r}
else
each{|r| (h[r[key_column]] ||= []) << r}
end
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.