lib/selections/belongs_to_selection.rb in selections-0.1.14 vs lib/selections/belongs_to_selection.rb in selections-0.2.1
- old
+ new
@@ -24,18 +24,21 @@
# Note that this is only appropriate to use for system selection values that are known
# at development time, and not to values that the users can edit in the live system.
def belongs_to_selection(target, options={})
belongs_to target, options.merge(:class_name => "Selection")
- prefix = self.name.downcase
- parent = Selection.where(system_code: "#{prefix}_#{target}").first
- if parent
- target_id = "#{target}_id".to_sym
- parent.children.each do |s|
- method_name = "#{s.system_code.sub("#{prefix}_", '')}?".to_sym
- class_eval do
- define_method method_name do
- send(target_id) == s.id
+ # The "selections" table may not exist during certain rake scenarios such as db:migrate or db:reset.
+ if ActiveRecord::Base.connection.table_exists? Selection.table_name
+ prefix = self.name.downcase
+ parent = Selection.where(system_code: "#{prefix}_#{target}").first
+ if parent
+ target_id = "#{target}_id".to_sym
+ parent.children.each do |s|
+ method_name = "#{s.system_code.sub("#{prefix}_", '')}?".to_sym
+ class_eval do
+ define_method method_name do
+ send(target_id) == s.id
+ end
end
end
end
end
end