DataMapper has_one problem
-
23-08-2019 - |
Question
I`m having trouble associating models in DataMapper. Its really simple, but i just can get the idea.
So, i have 2 tables:
1. Books
-> id
-> title
-> publisher_id
2. Publishers
-> id
-> title
The classes:
class Book
property :id, Serial
property :title, String
property :publisher_id, Integer
end
class Publisher
property :id, Serial
property :title, String
end
So, the question is: How can i connect publisher to book? It is 1-to-1 relationship, and the whole thing supposed to look like this:
p = Book.get(12345).publisher
Sorry, maybe it is stupid. But i just cant figure out what kind of declaration i should use.
No correct solution
OTHER TIPS
Haha, im crazy idiot sitting at 2 in the morning. Always happening to me, when i ask somethis - suddenly find answer for my question myself.
It is incorrect, there is one-to-many relationship. So, it is simple as sun in the sky:
class Book
property :id, Serial
property :title, String
property :publisher_id, Integer
belongs_to :publisher
end
class Publisher
property :id, Serial
property :title, String
has n, :books
end
That`s it. It might be helpful to somebody.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow