Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How do I modify Rails ActiveRecord query results before returning?
- MyModel.some_custom_scope.some_other_scope.enhance_with_external_data.each do |object|
- puts object.some_attribute_from_external_data_source
- end
- def self.enhance_with_external_data
- external_data = get_external_data
- Enumerator.new do |yielder|
- # mimick some stuff I saw in ActiveRecord and don't quite understand:
- relation.to_a.each do |obj|
- update_obj_with_external_data(obj)
- yielder.yield(obj)
- end
- end
- end
- MyModel.some_custom_scope.some_other_scope.enhance_with_external_data
- def self.merge_with_extra_info
- the_scope = scoped
- class << the_scope
- alias :base_to_a :to_a
- def to_a
- MyModel.enhance(base_to_a)
- end
- end
- the_scope
- end
- def self.enhance(items)
- items.each do |item|
- item = add_extra_item_info(item)
- end
- items
- end
- MyModel.where(:some_attribute => some_value).merge_with_extra_info.limit(10).all
Advertisement
Add Comment
Please, Sign In to add comment