2011-09-28 104 views
2

我有一组与arc_id(object_id)和arc_type(object_type)的global_identificiation表有关系的位置(即复合外键键)。我将如何建立关联,以便在每个位置查找中全面加载此全局信息?这样的:在Rails 3/ActiveRecord中建模的正确方法 - 复合外键

@l=Location.find(23) 

@l['id']=23 
@l['name']='some place' 
@l['global_info']['id']=145 
@l['global_info']['arc_id']=23 
@l['global_info']['arc_type']='Location' 

眼下,为省电,我只是做了一个after_save的回调是如何进入数据库,但没有任何其他公会吧。

THX任何帮助

编辑:
或许这可能仅仅是做作为需要它的阶级的after_find?

回答

0

要么使用after_find,要么只是在您的模型中创建一个方法来调用,然后只是将数据作为参数传递。或者更好的做一个模块,你所有的基于位置的类都可以包含,然后这个模块可以定义各种基于位置的方法。

module Locations 
    def find_with_data(id) 
     x = Location.find(id) 
     x['id']=id 
     x['name']='some place' #you could pass this in params 
     x 
    end 
    end 

class Location < ActiveRecord::Base 
    include Locations 
end 

然后调用

new_loc = Location.new 
new_loc.find_with_data(35)