I think that you can do like this
BookReader.first.books.all(:order => [:published.desc])
So, books is connected to BookReader through checkouts. You will take the books that are checkouts.
So long
2012/9/3 armanx
<arm...@gmail.com>
I'm looking to sort records in one table, based on a date column in another table. For example, I have the following tables:
class BookReader
include DataMapper::Resource
has n, :checkouts
has n, :books, :through => :checkouts
property :id, Serial
property :name, String
property :city, String
end
class Book
include DataMapper::Resource
has n, :checkouts
has n, :readers, :through => :checkouts
property :id, Serial
property :title, String
property :author, String
property :published, Date
end
class Checkout
include DataMapper::Resource
belongs_to :bookreader, :key => true
belongs_to :book, :key => true
property :created_at, DateTime
property :updated_at, DateTime
end
(This assumes each BookReader can only check out each Book once).
I can select all the Book objects belonging to a particular BookReader:
checkouts = BookReader.first.checkouts
Now, I want to sort these based on the :published date of the Book object. E.g.:
SELECT
c.*, b.date
FROM
"checkouts" as c
JOIN "books" as b on b.id = c.book_id
WHERE
c.bookreader_id = 1
ORDER BY
b.date DESC
Any way to do this directly through DM?
--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/SS4rSLbWrKQJ.
To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.
--
------------------------------------------
1. Notebook para você ficar livre e programar a vontade: R$ 2300
2. Curso de Programação/SysAdmin/DBA: R$ 5000
3. Pedir a solução pronta para um problema numa lista de discussão: Não tem preço !
E para todas as outras existe RTFM, STFW e LMGTFY