使用活动脚手架在 Rails 中加入三列 [英] Three Column Join in Rails with Active Scaffold

查看:26
本文介绍了使用活动脚手架在 Rails 中加入三列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列的连接表.我试过用 a has many through 和 habtm 来定义这三个模型.我将主动脚手架仅用于 CRUD 功能,然后它将被逐步淘汰.我有一种感觉,如果我想要一个三列连接表,我将不得不尽早淘汰活动脚手架并编写自定义帮助程序.

I have a join table with three columns. I've tried defining the three models with both a has many through and habtm. I am using active scaffold just for CRUD functionality then it will get phased out. I have a feeling that if I want a three column join table, I'm going to have to phase out active scaffold early and write a custom helper.

(我丈夫在 SQL 方面有多年经验,他同意 3 列连接表是正确的方法.他没有任何使用 Rails 的经验.)

(My husband has years of experience with SQL, and he agrees that a 3 column join table is the correct approach. He doesn't have any experience with rails.)

我的三个模型是:员工、项目、角色,其中角色是项目中的员工角色.我的表是雇员_项目_角色

My three models are: Employee, Project, Role where Role is the employees role in the project. My table is employees_projects_roles

在 Rails 中执行此操作的 DRY 和其他有效方法是什么?如果有办法.

What is the DRY and otherwise efficient way of doing this in rails? If there is a way.

除了模型中注释掉的 habtm 和 hmt 以及控制器中的 active scaffold 语句之外,我没有更多内容.

I don't have much more than the commented out habtm and the hmt in the models and the active scaffold statement in the controllers.

有趣的错误:|在所有三个主要模型以及 EmployeeProjectRole 模型中使用 hmt 方法接收:

Fun Error :| Received using the hmt method in all three main models as well as the EmployeeProjectRole model:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.klass

Extracted source (around line #5):

2:   <tr>
3:     <%
4:     active_scaffold_config_for(@record.class).subform.columns.each :for => @record, :flatten => true do |column|
5:       next unless in_subform?(column, parent_record) and column_renders_as(column) != :hidden
6:       -%>
7:       <th<%= ' class="required"' if column.required? %>><label><%= column.label %></label></th>
8:     <% end -%>

发现这篇文章 关于三元关联(使用这个词在谷歌上进一步搜索,没有其他结果).在主要模型中,我注释掉了文章后面的第一行:

Found this article on ternary associations (further searching on Google using this term came up with nothing else). In the main models I commented out the first line to follow the article:

#    has_many        :employees_projects_roles
    has_many        :roles,     :through => :employees_projects_roles
    has_many        :projects,  :through => :employees_projects_roles

我收到了这个错误:

ActiveRecord::HasManyThroughAssociationNotFoundError in EmployeesController#index
Could not find the association :employees_projects_roles in model Employee

这似乎不是一个 active_scaffold 错误,而是一个 ActiveRecord 错误.这似乎意味着 has_many :employees_projects_roles 需要在那里,尽管文章建议.可能是 2008 年的 API 变化?

which doesn't seem to be an active_scaffold error, but an ActiveRecord error. This seems to imply that has_many :employees_projects_roles needs to be there, despite what the article suggests. API change from 2008 maybe?

推荐答案

我发布了一个相关问题 此处.答案涉及在模型中声明关联时使用 :join_table.

I posted a related question here. The answer involved using :join_table when declaring associations in the model.

错误消息nil object"我涉及到的对象没有被实例化.有时,当表中没有数据时,似乎会出现该消息.

The error message "nil object" I was getting involved the object not getting instantiated. Sometimes, it seems the message comes up when there's no data in the table.

最后一条错误信息,我没有答案.

The last error message, I have no answer for.

感谢您的回复.SO 是问我是否要悬赏,但我认为这是您刚刚遇到的问题之一,并且在解决其他问题时它会消失.有时,继续前进比试图找出原因更务实.

Thanks for your responses. SO is asking if I want to place a bounty, but I think this is one of those problems that you just get and in solving something else it goes away. Sometimes, moving on is more pragmatic than trying to find out why.

这篇关于使用活动脚手架在 Rails 中加入三列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆