Forum: Ruby ActiveRecord :conditions with .attributes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C0f4cef363d69500730aa9afa9cf7774?d=identicon&s=25 dkmd_nielsen (Guest)
on 2008-10-31 18:57
(Received via mailing list)
Thanks for any help and suggestions.  I'm having troubles with the
following not working.  It generates a "miss value for :OCRACR in
OCRACR = :OCRACR..." error.

slct = "OCRACR = :OCRACR and MSG = :MSG and KEYCODE = :KEYCODE and
VERSION = :VERSION and DEPTH = :DEPTH "

unq_rows = Entry.find_by_sql(
   "select distinct #{flds} from entries "
).each {|e|
   o = Entry.find(:all,:conditions => [slct,e.attributes])
   puts o.length
}

When I do "pp e.attributes", it displays the following hash:

{"OCRACR"=>"O", "MSG"=>"", "KEYCODE"=>"212260", "VERSION"=>"01",
"DEPTH"=>"001"}

Shouldn't the find :conditions be ok?  The slct string appears
formatted ok.  The .attributes hash appears to match the :conditions
string.  I'm at a loss for what I am doing incorrectly.

Thanks again,
dvn
C0f4cef363d69500730aa9afa9cf7774?d=identicon&s=25 dkmd_nielsen (Guest)
on 2008-10-31 19:25
(Received via mailing list)
On Oct 31, 12:54 pm, dkmd_nielsen <d...@cmscms.com> wrote:
>    o = Entry.find(:all,:conditions => [slct,e.attributes])
> formatted ok.  The .attributes hash appears to match the :conditions
> string.  I'm at a loss for what I am doing incorrectly.
>
> Thanks again,
> dvn

And I'll follow this up with the following that does function:

unq_rows = Entry.find_by_sql(
   "select distinct #{flds} from entries "
).each {|e|
   slct = ""
   e.attributes.each {|key,value|
      slct << "#{key} = '#{value}' and " if key != 'id'
      }
   o = Entry.find(:all,:conditions => slct[0,slct.length-4] )
   puts "#{e.KEYCODE} #{o.length}"
}

I'm sure the error is related to :KEYCODE versus 'KEYCODE' in the hash
key.

Let me ask a simpler question:  What is the easiest method for getting
the COUNT of rows for each distinct set of column values.  In the
above case, I want to get the count of rows for each unique
combination of column values.  Is there a simpler sql solution?

Thanks again for any responses,
dvn
This topic is locked and can not be replied to.