My model setup
class User < AR
has_many :books
...
end
class Book < AR
belongs_to :user
...
end
Books are in separate apartments and users are in the public schema.
I would like to add a shared model called assignment (to assign a book to a user) in the public schema that will have the following setup
class User < AR
has_many :books
has_many :assignments
...
end
class Book < AR
belongs_to :user
has_one :assignment
...
end
class Assignment < AR
belongs_to :user
belongs_to :book
...
end
The problem lies with the belongs_to :book
portion in that the Assignment model is shared, giving no way to easily specify the schema that the book is in. There is also the possibility of id collisions among the books across different schemas which complicates the process further. As I see it I could:
1) Use scopes, specifying the apartment schema for the book
2) Use custom getters and setters for the book attribute in assignment, and fetch the required objects in the getter.
3) Put the assignment foreign key in the books model, which would be the simplest but not as clean (I want to be able to get the book_id/schema without a join)
4) Distribute the assignments model to the apartment schemas, which would be difficult as I must collect the assignments and sync them to an outside data source.
What is the best solution in this situation?