实现

2012-12-24 38 views
-1

我有员工模型,其中我有很多控制器 像实现

jobs_controller.rb 
contacts_controller.rb 
personals_controller.rb 
dependets_controller.rb 

它们都与员工控制器。 我正在使用MongoDb,因为我有不同的控制器,我也有不同的模型。 在我的仪表板中,我必须显示相关的员工详细信息。其中一个字段来自联系人控制器,另一个来自dependents_controller,另一个来自个人控制器。 在这里,我必须调用所有模型并从每个模型中取一个字段。 我可以通过显示来自一个相关模型的每个字段来自定义此代码。 我正在使用设计。不能我存储每个用户相关数据的ID和调用通过用户模型? 我搞砸了..如果是的话那怎么样? 在我的员工控制器

def index 
    @employees = Employee.all 
    Employee.includes(:dependants).each do |dependant| 
    p dependant.firstname #example of pulling data for that related entity 
    end 


    end 

而且我怎么能定位到相关人员的数据?

回答

2

看看这部分的mongoid文档:http://mongoid.org/en/mongoid/docs/relations.html 它应该给你一个很好的指示你如何在你的模型中实现关系逻辑。

不过,我强烈建议你阅读这首: http://docs.mongodb.org/manual/core/data-modeling

特别是参考和原子部分

我过去抓出来不能完全把握蒙戈和交易数据库引擎(如InnoDB的),它使我通过一个项目重新设计我的车型中旬方式之间的差异。

class Employee 
    include Mongoid::Document 
    field :address, type: String 
    field :work, type: String 
    has_many :dependants 
end 

class Dependant 
    include Mongoid::Document 
    field :firstname, type: String 
    field :lastname, type: String 
    belongs_to :employee 
end 

在你的控制,那么你可以通过员工访问家属数据:

根据要求

由我的第一个链接描述你可以设置你的模型像这样的澄清

#this example loops through every employee in the collection 
Employee.includes(:dependants).each do |employee| 
    employee.dependants.each do |dependant| 
    p dependant.firstname #example of pulling data for that related entity 
    end 
end 
+0

可以给我一些与我的控制器相关的编码示例吗?如果是这样,那么它会男性我快速赶上我的答案 – regmiprem

+0

我试过了..但我没有得到它。假设我有员工场2场:地址,类型:字符串 领域:工作类型:字符串和两个领域依赖字段:姓名,类型:字符串 领域:名字,类型:字符串那我怎么才能实现,在型号并且也在员工控制器中。你可以编辑代码吗?由于 – regmiprem

+0

员工的我的索引控制器是这样的高清指数 @employees = Employee.all Employee.includes(:家属)。每个办|依赖|格式|拉力数据为相关实体 结束 的respond_to做的 p dependant.firstname#示例| format.html#index.html.erb format.json {渲染JSON:@employees} 结束 结束它给错误未定义的方法'姓”为#<员工:0x0000000201a2f0> – regmiprem