From a80f70bb8820d041755802d91f9ff6c5b60c9cad Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 5 Mar 2013 20:51:24 -0600 Subject: [PATCH] phew, fixed some issues with contribution saving in rails 3.2 --- app/models/contribution.rb | 6 +++--- app/models/item.rb | 2 +- app/models/pet_state.rb | 3 ++- app/models/pet_type.rb | 2 +- app/models/swf_asset.rb | 2 +- app/models/user.rb | 12 +++++++----- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/models/contribution.rb b/app/models/contribution.rb index 6ce7d4f1..ffa79d02 100644 --- a/app/models/contribution.rb +++ b/app/models/contribution.rb @@ -5,8 +5,6 @@ class Contribution < ActiveRecord::Base 'PetType' => 15, 'PetState' => 10 } - - attr_accessor :contributed belongs_to :contributed, :polymorphic => true belongs_to :user @@ -17,7 +15,9 @@ class Contribution < ActiveRecord::Base @@per_page = 30 def point_value - POINT_VALUES[contributed_type] + POINT_VALUES[contributed_type] || + raise("unexpected contributed type #{contributed_type.inspect} for " + + "contributed #{contributed.inspect}") end CONTRIBUTED_RELATIONSHIPS = { diff --git a/app/models/item.rb b/app/models/item.rb index 185131c4..ad823225 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -9,7 +9,7 @@ class Item < ActiveRecord::Base translates :name, :description, :rarity has_many :closet_hangers - has_one :contribution, :as => :contributed + has_one :contribution, :as => :contributed, :inverse_of => :contributed has_many :parent_swf_asset_relationships, :as => :parent has_many :swf_assets, :through => :parent_swf_asset_relationships diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index b2bf06b8..37427a50 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -1,7 +1,8 @@ class PetState < ActiveRecord::Base SwfAssetType = 'biology' - has_many :contributions, :as => :contributed # in case of duplicates being merged + has_many :contributions, :as => :contributed, + :inverse_of => :contributed # in case of duplicates being merged has_many :outfits has_many :parent_swf_asset_relationships, :as => :parent, :autosave => false diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index 0b501dbb..7b730368 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -5,7 +5,7 @@ class PetType < ActiveRecord::Base belongs_to :species belongs_to :color - has_one :contribution, :as => :contributed + has_one :contribution, :as => :contributed, :inverse_of => :contributed has_many :pet_states has_many :pets diff --git a/app/models/swf_asset.rb b/app/models/swf_asset.rb index 9582156b..f123eab0 100644 --- a/app/models/swf_asset.rb +++ b/app/models/swf_asset.rb @@ -143,7 +143,7 @@ class SwfAsset < ActiveRecord::Base attr_accessor :item - has_one :contribution, :as => :contributed + has_one :contribution, :as => :contributed, :inverse_of => :contributed has_many :parent_swf_asset_relationships delegate :depth, :to => :zone diff --git a/app/models/user.rb b/app/models/user.rb index 31797fe0..608aacb3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,25 +19,27 @@ class User < ActiveRecord::Base def contribute!(pet) new_contributions = [] - new_points = 0 pet.contributables.each do |contributable| if contributable.new_record? - contribution = Contribution.new(:contributed => contributable, - :user => self) + contribution = Contribution.new + contribution.contributed = contributable + contribution.user = self new_contributions << contribution - new_points += contribution.point_value end end - self.points += new_points + new_points = 0 # temp assignment for scoping Pet.transaction do pet.save! new_contributions.each do |contribution| + Rails.logger.debug("Saving contribution of #{contribution.contributed.inspect}: #{contribution.contributed_type.inspect}, #{contribution.contributed_id.inspect}") begin contribution.save! rescue ActiveRecord::RecordNotSaved => e raise ActiveRecord::RecordNotSaved, "#{e.message}, #{contribution.inspect}, #{contribution.valid?.inspect}, #{contribution.errors.inspect}" end end + new_points = new_contributions.map(&:point_value).inject(0, &:+) + self.points += new_points begin save! rescue ActiveRecord::RecordNotSaved => e