Skip to content

Instantly share code, notes, and snippets.

@masterkain
Created September 3, 2008 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masterkain/8621 to your computer and use it in GitHub Desktop.
Save masterkain/8621 to your computer and use it in GitHub Desktop.
strange behaviour on saving rich join attributes
class Report < ActiveRecord::Base
has_many :report_parameters
has_many :parameters, :through => :report_parameters do
def push_with_attributes(monitored_parameter, rich_join_attributes)
ReportParameter.send(:with_scope, :create => rich_join_attributes) { self << monitored_parameter }
end
end
# "report_parameter_attributes"=>[{"value"=>"10", "parameter_id"=>"14"}]
def report_parameter_attributes=(parameter_attributes)
parameters.clear
parameter_attributes.each do |attributes|
parameters.push_with_attributes(Parameter.find(attributes[:parameter_id].to_i), { :value => attributes[:value] })
end
end
end
# if self(Report) is a new record join attribute is not stored upon creation.
# INSERT INTO `report_parameters` (`created_at`, `updated_at`, `report_id`, `value`, `parameter_id`) VALUES('2008-09-03 16:59:42', '2008-09-03 16:59:42', 5, NULL, 1)
# on update:
# INSERT INTO `report_parameters` (`created_at`, `updated_at`, `report_id`, `value`, `parameter_id`) VALUES('2008-09-03 17:00:06', '2008-09-03 17:00:06', 5, 'no', 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment