I have 2 tables, products and codes. Products has a code_str column. I can find a Code from a product by using the first 2 characters of the products.code_str column. I’ve tried doing something like this:
class Product < ActiveRecord::Base
belongs_to :code, foreign_key: "substring(products.code_str from 1 for 2)", primary_key: :number
end
But it doesn’t work. I’d like to use some eager loading in the future, so I thought on making a belongs_to association work instead of anything else.
Any ideas?
Had the same problem.
At the end added:
Pros: finds each and works with include
Cons: Queries are slow when using include