i can manage the MySQL queries into active record.but in some cases
basically with sub-queries.i didn't get the correct output.
here when i trying to change the MySQL queries into active-record.

*MySQL Query*

*select count(*) from ( SELECT transactions.loyalty_id FROM transactions
JOIN loyalties ON (transactions.loyalty_id = loyalties.id) where
loyalties.created_at > '2011-11-02' GROUP BY transactions.loyalty_id HAVING
COUNT(*)>1 ) as t*

Here just i tried to convert into corresponding active record queries. But
i didn't get the correct output.

*Transaction.count(:loyalty_id) , :joins=> ["JOIN loyalties ON
(transactions.loyalty_id = loyalties.id)" ] , :conditions =>
["loyalties.created_at > ? ",cdate], :group=> ["transactions.loyalty_id"],
:having=> ["count(*) > 1 "] )*

How to change that issue?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/VT9PqxKYyLEJ.
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

  • Priyanka Pathak at Dec 29, 2011 at 1:10 pm
    Hi,
    Modify your query to

    loyalties = Transaction.count(:loyalty_id , :joins=> :loyalties,
    :conditions => ["loyalties.created_at > ? ",cdate], :group=>
    ["transactions.loyalty_id"], :having=> ["count(*) > 1 "]

    loyalties contain OrderedHash. Now do loyalties.count to get actual
    count of filtered loyalties and that matches with your raw sql output.

    --
    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
postedDec 27, '11 at 2:16p
activeDec 29, '11 at 1:10p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Priyanka Pathak: 1 post Amvis: 1 post

People

Translate

site design / logo © 2021 Grokbase