Rails 3.2.3, Ruby 1.9.3p194
trying to save a Project with HABTM-associated subprojects, I overwrite the default generated SQL, with calling the Name attribute of the mother project and each associated subproject. How do I specify correctly the associated subprojects Name? #{record.Name}, as in the example, leads to NameError: undefined local variable or method `record' for #
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}" )} }
Any Suggestions?
solved, from Rails 3.1. Release Notes:
Previously :insert_sql and :delete_sql on has_and_belongs_to_many association allowed you to call ‘record’ to get the record being inserted or deleted. This is now passed as an argument to the proc.
Thus:
:insert_sql => proc { |record| %{INSERT INTO `project_subproject` (`projectname`, `subprojectname`) VALUES ( "#{self.Name}","#{record.Name}" )} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With