I would like to establish a relationship between two Mongolian models with has_many and belongs_to and specify a foreign key, for example:
class Author
include Mongoid::Document
field :serial_num, :type => Integer
field :author_name, :type => String
has_many :books
end
class Book
include Mongoid::Document
field :serial_num, :type => Integer
field :book_name, :type => String
belongs_to :author, foreign_key: 'serial_num'
end
This does not work. My IRB output:
irb :001> b = Book.first
=> #<Book _id: 1, serial_num: "12345", book_name: 'something', author_id: nil>
irb :002> b.author
=> nil
Is it possible to specify 'serial_num' as a foreign key for this relationship, or am I stuck with author_id?
Many thanks.
source
share