0

I have an "Factory module" which has "has_many" relationship with "Icecream Model". I have done mixin of factory module with another "Owner model" so that i can have a relationship of "owner" with "icecreams table". Problem is I am not able to fetch icecreams with the "owner" object i think something is wrong with the relationship definition. The definition from Factory model is below.

module Factory
  extend ActiveSupport::Concern

  included do    
    has_many :icecreams, :dependent => :nullify 
  end

The relationship definition from Icecream is like this

Class Icecream < ActiveRecord::Base

belongs_to :factory, :class_name => "Owner", :foreign_key => "factory_id"
4

2 回答 2

0

解决了。必须在双方都定义关联(has_many,belongs_to)。我在belongs_to 端定义了foreign_key: "factory_id"。现在,从 has_many 方面来看,我希望我的关联使用带有“factory_id”的look_up,因此我也必须在 has_many 方面创建foreign_key:“factory_id”。

module Factory
  extend ActiveSupport::Concern

  included do    
    has_many :icecreams, :dependent => :nullify, :foreign_key => "factory_id"
  end
于 2013-10-16T09:14:51.323 回答
0

试试这个,我希望它对你有用。

module Factory   extend ActiveSupport::Concern
  def self.included(base)> 
     base.instance_eval("has_many :icecreams, :dependent => :nullify )   
  end
于 2013-10-15T10:25:57.533 回答