Skip to content

Mongoid strange ruby behavior #5776

Closed Answered by jamis
CharlesPlantePulsar asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @CharlesPlantePulsar -- this is a tricky one. The problem is that the belongs_to macro creates getter methods both for the association, and for the foreign key. In this case, both are named the same thing, so it creates the client accessor for the association first, and then overwrites it with the client accessor for the foreign key.

In this case, you may need to rename the association to something else, so that it doesn't collide with the foreign key. For example:

class User
  include Mongoid::Document
  belongs_to :client_record, class_name: "Client", foreign_key: :client
end

It's not ideal, but working with legacy systems can be like that. :/

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@CharlesPlantePulsar
Comment options

Answer selected by CharlesPlantePulsar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants