Hello 

First of all, if there are 2 models ... then i think you got it wrong ...  
assuming you have 


class Book < ActiveRecord::Base 
has_many :likes
has_one :book_expire
end

class BookExpire <  ActiveRecord::Base 
belongs_to :book
end

class Like < ActiveRecord::Base 
belongs_to :book
end

Then you could add " , :counter_cache => true " to the belongs_to association 
(if needed add a likes_count field in the Book table)

if you do that .., then you could do a very easy query like 

@books = Book.joins(:book_expire).all.order('  likes_count ASC, 
book_expires.end_at DESC')  # and i would add a limit ()

have a look here :   http://railscasts.com/episodes/23-counter-cache-column 
(even the move uses rails 2.x ... the concept and syntax is still valid )

Alecslupu

On Feb 3, 2012, at 9:44 PM, Abhishek shukla wrote:

> Hello Friends,
> 
> I have a rails application where requirement is that, I want to display books 
> based on likes and expiry date..
> 
> I am using the following query
> 
> "select books.*, (select count(*) from likes where likes.book_id = books.id) 
> as ct
> from books, book_expires where book_expires.book_id = books.id order by  ct 
> ASC, book_expires.end_at DESC"
> 
> And the output I am getting is on the bases of count not end_date..
> 
> Please let me know where I am getting wrong thanks...
> 
> 
> Thanks
> abhis
> 
> 
> -- 
> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.

Attachment: PGP.sig
Description: This is a digitally signed message part

Reply via email to