Skip to content
Browse files

Make sure that belongs_to association ids only appear once in the out…

…put, and DRY up the generation of the remote id attribute.
  • Loading branch information...
1 parent 7a1fab3 commit 6a68ab2e772e43d372161491b7b3849d4912e4cd @craigw committed Jan 2, 2009
Showing with 4 additions and 16 deletions.
  1. +4 −9 lib/active_couch/associations/belongs_to_association.rb
  2. +0 −7 lib/active_couch/base.rb
View
13 lib/active_couch/associations/belongs_to_association.rb
@@ -7,15 +7,10 @@ def include_in_document?
end
def add_association_methods(base)
- base.instance_eval <<-EOF
- def #{@association_name}_id
- @attributes[:#{@association_name}]
- end
-
- def #{@association_name}_id=(#{@association_name}_id)
- @attributes[:#{@association_name}] = #{@association_name}_id
- end
-
+ base.class.instance_eval <<-EOF, __FILE__, __LINE__
+ has :#{@association_name}_id
+ EOF
+ base.instance_eval <<-EOF, __FILE__, __LINE__
def #{@association_name}
@#{@association_name} ||= #{associated_class.name}.find(#{@association_name}_id)
end
View
7 lib/active_couch/base.rb
@@ -86,13 +86,6 @@ def to_json
hash = {}
# First merge the attributes...
hash.merge!(attributes.reject{ |k,v| v.nil? })
- # ...and then the associations
- associations.each_pair do |name, proxy|
- if proxy.respond_to?(:include_in_document?) && proxy.include_in_document?
- value = self.__send__(name.to_s)
- hash.merge!({ "#{name}_id" => value.id })
- end
- end
# ... and then the document type
hash.merge!(:type => self.class.document_type)
# and by the Power of Grayskull, convert the hash to json

0 comments on commit 6a68ab2

Please sign in to comment.
Something went wrong with that request. Please try again.