Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #49 from superjustin/master

Fixes two relationship issues: has_one and has_many
  • Loading branch information...
commit 7db00dd8b403e810a310836dd70ddac4411c3ffc 2 parents 5f3d639 + a3b1338
@treeder treeder authored
Showing with 14 additions and 17 deletions.
  1. +11 −16 lib/simple_record/attributes.rb
  2. +3 −1 lib/simple_record/validations.rb
View
27 lib/simple_record/attributes.rb
@@ -219,24 +219,19 @@ def belongs_to(association_id, options = {})
define_dirty_methods(arg_s)
end
- def has_many(*args)
- args.each do |arg|
- #okay, this creates an instance method with the pluralized name of the symbol passed to belongs_to
- send(:define_method, arg) do
- #when called, the method creates a new, very temporary instance of the Activerecordtosdb_subrecord class
- #It is passed the three initializers it needs:
- #note the first parameter is just a string by time new gets it, like "user"
- #the second and third parameters are still a variable when new gets it, like user_id
- return eval(%{Activerecordtosdb_subrecord_array.new('#{arg}', self.class.name ,id)})
- end
+ # allows foreign key through class_name
+ # i.e. options[:class_name] = 'User'
+ def has_many(association_id, options = {})
+
+ send(:define_method, association_id) do
+ return eval(%{Activerecordtosdb_subrecord_array.new('#{options[:class_name] ? options[:class_name] : association_id}', '#{options[:class_name] ? association_id.to_s : self.class.name}', id)})
end
- #Disclaimer: this whole funciton just seems crazy to me, and a bit inefficient. But it was the clearest way I could think to do it code wise.
- #It's bad programming form (imo) to have a class method require something that isn't passed to it through it's variables.
- #I couldn't pass the id when calling find, since the original find doesn't work that way, so I was left with this.
end
- def has_one(*args)
-
+ def has_many(association_id, options = {})
+ send(:define_method, association_id) do
+ return eval(%{Activerecordtosdb_subrecord_array.new('#{options[:class_name] ? options[:class_name] : association_id}', '#{options[:class_name] ? association_id.to_s : self.class.name}', id)}).first
+ end
end
@@ -437,4 +432,4 @@ def init_value(value)
end
end
-end
+end
View
4 lib/simple_record/validations.rb
@@ -71,7 +71,9 @@ def validate_uniques
val = self.send(k)
puts 'val=' + val.inspect
if val
- ret = self.class.find(:first, :conditions=>["#{k}=?", val])
+ conditions = new_record? ? ["#{k}=?", val] : ["#{k}=? AND id != ?", val, self.id]
+
+ ret = self.class.find(:first, :conditions=>conditions)
puts 'ret=' + ret.inspect
if ret
errors.add(k, "must be unique.")
Please sign in to comment.
Something went wrong with that request. Please try again.