I read in the Rails 3 Way book that when using select(), you can, in
addition to adding calculated columns (i.g. using sql aggregate
functions), include additional attributes in resulting object by
passing the wild card like this:

Unit.select(:*,"sum(unit_type_id) as
total").group("created_at").having(["created_at > ?", 2.days.ago])


That should give you the new method "total" in addition to the default
attributes of the object. However, when I do it, I get this:


ArgumentError: wrong number of arguments (2 for 1)

It appears the select method not accepting multiple parameters?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Robert Walker at May 15, 2012 at 2:25 pm

    John Merlino wrote in post #1060601:
    I read in the Rails 3 Way book that when using select(), you can, in
    addition to adding calculated columns (i.g. using sql aggregate
    functions), include additional attributes in resulting object by
    passing the wild card like this:

    Unit.select(:*,"sum(unit_type_id) as
    total").group("created_at").having(["created_at > ?", 2.days.ago])

    That should give you the new method "total" in addition to the default
    attributes of the object. However, when I do it, I get this:

    ArgumentError: wrong number of arguments (2 for 1)
    As far as I can tell from the docs Model.select takes one argument in
    all forms:

    http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-select

    So that error makes sense as far as I can tell.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Robert Walker at May 15, 2012 at 2:28 pm

    Robert Walker wrote in post #1060823:
    Unit.select(:*,"sum(unit_type_id) as
    total").group("created_at").having(["created_at > ?", 2.days.ago])
    Maybe you meant this:

    Unit.select([ :*, "sum(unit_type_id) as total"
    ]).group("created_at").having(["created_at > ?", 2.days.ago])

    The one argument can be an array, which would be the common case.

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMay 13, '12 at 4:25p
activeMay 15, '12 at 2:28p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Robert Walker: 2 posts John Merlino: 1 post

People

Translate

site design / logo © 2021 Grokbase