Browse files

- allow foreign keys for has_many

  • Loading branch information...
1 parent 792ee32 commit 20995142c59848ce04a71cddca8252b1ce4b3351 @superjustin superjustin committed May 16, 2012
Showing with 6 additions and 13 deletions.
  1. +6 −13 lib/simple_record/attributes.rb
@@ -219,20 +219,13 @@ def belongs_to(association_id, options = {})
- 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(%{'#{arg}', ,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(%{'#{options[:class_name] ? options[:class_name] : association_id}', '#{options[:class_name] ? association_id.to_s :}', id)})
- #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.
def has_many(association_id, options = {})

0 comments on commit 2099514

Please sign in to comment.