gpt4 book ai didi

ruby-on-rails - HABTM-协会Insert_sql

转载 作者:行者123 更新时间:2023-12-03 22:40:01 26 4
gpt4 key购买 nike

rails 3.2.3, ruby 1.9.3p194

尝试用 HABTM 相关子项目保存一个项目,我覆盖默认生成的 SQL,调用母项目和每个相关子项目的名称属性。如何正确指定关联的子项目名称? #{record.Name},如示例中所示,导致名称错误: #

的未定义局部变量或方法“record”
has_and_belongs_to_many :subproject, :class_name => "Project",
:join_table => "project_subproject", :foreign_key => "projectname", :association_foreign_key => "subprojectname",
:finder_sql => proc { %{SELECT `project`.* FROM `project` INNER JOIN `project_subproject` ON `project`.`name` = `project_subproject`.`subprojectname` WHERE `project_subproject`.`projectname` = '#{self.Name}' } },
#TODO: find correct expression for insert sql
:insert_sql => proc { %{INSERT INTO `project_subproject` (`projectname`, `subprojectname`) VALUES ( "#{self.Name}","#{record.Name}" )} }

有什么建议吗?

最佳答案

已解决,来自 Rails 3.1。发行说明:

以前,has_and_belongs_to_many 关联上的 :insert_sql 和 :delete_sql 允许您调用“record”来获取正在插入或删除的记录。这现在作为参数传递给 proc。

因此:

:insert_sql =>  proc { |record| %{INSERT INTO `project_subproject` (`projectname`, `subprojectname`) VALUES ( "#{self.Name}","#{record.Name}" )} }

关于ruby-on-rails - HABTM-协会Insert_sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12217630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com