Skip to content

Association with different foreign keys #501

prajwalkman opened this Issue Mar 6, 2013 · 2 comments

2 participants



I have some legacy tables that don't follow the mysql/rails best practices. Essentially, I have 2 associated tables abc and def. def has a key weird_name_id which actually references a row in abc.

I used to use the following in factory girl:

factory :def do
  association :weird_name_id, factory: :abc

This was working fine, but I recently upgraded my app to rails 3.2.9+, and caused a regression and the above factory started throwing errors of the form:
undefined method 'to_i' for #<abc:0x0021930123>

My understanding of the problem led me to believe that this could be overcome if I could do the following in factorygirl:
association :abc, foreign_key: :weird_name_id

If my thinking is right, how do I achieve this? And if not, how do I get around this?


I've temporarily worked around this by defining a to_i method for the offending models, which return the object's id.

thoughtbot, inc. member

@prajwalkman IIRC this is actually an issue with 3.2.9; you should update to 3.2.12 (or 3.2.13 next week when it's released) and remove the to_i hack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.