lib/api_resource/associations.rb in api_resource-0.6.2 vs lib/api_resource/associations.rb in api_resource-0.6.3

- old
+ new

@@ -142,21 +142,21 @@ end end # TODO: add a special foreign_key option to associations def association_foreign_key_field(assoc, type = nil) - + if type.nil? && has_many?(assoc) type = :has_many else type = type.to_s.to_sym end # for now just use the association name str = assoc.to_s.singularize.foreign_key - if type == :has_many + if type.to_s =~ /^has_many/ str = str.pluralize end str.to_sym end @@ -226,22 +226,25 @@ self.#{assoc_name}.internal_object.present? end def #{id_method_name} @attributes_cache[:#{id_method_name}] ||= begin - if @attributes.key?(:#{id_method_name}) - @attributes[:#{id_method_name}] - else + if @attributes.has_key?("#{id_method_name}") + @attributes["#{id_method_name}"] + elsif self.#{assoc_name}.collection? self.#{assoc_name}.collect(&:id) + else + self.#{assoc_name}? ? self.#{assoc_name}.id : nil end end end def #{id_method_name}=(val, force = false) unless @attributes_cache[:#{id_method_name}] == val #{id_method_name}_will_change! end @attributes_cache[:#{id_method_name}] = val + write_attribute(:#{id_method_name}, val) end EOE end