[eluser]WanWizard[/eluser]
When defining relations, you'll have to view it from both sides.
A Province can have many books, but does a specific Book only belong to one Province, or can it be linked to multiple Provinces at once?
If only one, it's Province has_many Book, and Book has_one Province. Normalisation rules say that the foreign key (province_id) should be in the Book record, and no junction table is needed. If multiple, its Province has_many Book, and Book has_many Province, and you need a junction table with both the foreign keys (province_id and book_id) to link the two models.
If you have values that are a property of the relation itself, there are three options:
1) add the property (rate_pdf_file) to the junction table, and use the join_fields() methods of Datamapper to access or set them.
2) create a model for the junction table, and create a one to many relation between it and Provinces and Books so you can access it as any other model
3) create a separate model (like you propose)
Number 1 is the preferred solution, and it works out of the box and transparently.
For number 3, you need to query it manually, as Datamapper doesn't support compound keys, so you can't make a relation on two foreign keys.
What you can do is create a relation on both individually (Province -> Rates and Book -> Rates), and query with an extra where clause:
Code:
$book = new Book(1);
$rate = $book->rate->where('province_id', $provid);
But I would not recommend that.