belonogs_to modules and foreign_key #14320

viniciusgati opened this Issue Mar 8, 2014 · 1 comment


None yet
1 participant

Hi i have models inside a module :

class SearchEngine::KeywordScore < ActiveRecord::Base
  belongs_to :keyword, class_name: '::SearchEngine::Keyword'
  belongs_to :treatment, foreign_key: :model_id

and models outside any module like:

class Treatment < ActiveRecord::Base
  has_many :keywords, through: :treatment_key_score, class_name: 'SearchEngine::Keyword'
  has_many :keyword_scores, class_name: 'SearchEngine::KeywordScore'
  before_create :generate_keywords_score


  def generate_keywords_score
    words       = description.split(' ')
    uniq_words  = words.dup.uniq
    uniq_words.each do |word|
      keyword = SearchEngine::Keyword.where( key: word ).first_or_create keyword: keyword, 
                           model_name: self.class.model_name.to_s, 
                           field: 'description', 
                           score: words.count(word)

when i save a Treatment i populate the relation with some keyword scores, the problem is, i get a message like field 'treatment_id' does not exist, them i created a field 'treatment_id' in score table and everything works fine, but the deserved field for the relation is model_id field in scores table.

i dont know if im using this right or its some kind of a bug
Im using rails 4.0.3

wow my mistake, i need to configure the foreign_key on has_many(treament class) to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment