Created
March 19, 2015 08:13
-
-
Save metalelf0/dd6e882bd49b68bd5821 to your computer and use it in GitHub Desktop.
Array#reactiverecordize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # array_reactiverecordize_spec.rb | |
| require 'spec_helper' | |
| describe "Array#reactiverecordize" do | |
| let!(:john_doe) { create(:user, first_name: "John", last_name: "Doe", birth_year: '1966')} | |
| let!(:mike_pike) { create(:user, first_name: "Mike", last_name: "Pike", birth_year: '1996')} | |
| let!(:carl_doe) { create(:user, first_name: "Carl", last_name: "Doe", birth_year: '1926')} | |
| subject { User.where(last_name: 'Doe').select { |u| u.birth_year == 1926} } | |
| specify { expect(subject).to be_an Array } | |
| specify { expect(subject).to include(carl_doe) } | |
| # this raises undefined method `where' for []:Array | |
| specify { expect{subject.where(first_name: "Carl")}.to raise_error } | |
| # but this works | |
| specify { expect(subject.reactiverecordize.where(first_name: "Carl")).to include(carl_doe) } | |
| end | |
| # lib/core_ext/array.rb | |
| class Array | |
| def reactiverecordize | |
| class_of_first = self[0].class | |
| ids = self.map(&:id) | |
| class_of_first.where(id: ids) | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a fix for a situation that arises when you try to chain active_record scopes and in-memory operations like
selects, array intersection/join, and so on.It tries to convert an array back to an
ActiveRecordscope by refiltering on ids.The code inside
Array#reactiverecordizeis just a proof of concept, it requires some fixes like:Arrayare of the same class (to avoid mixing pears and apples);ActiveRecord::Base;[].reactiverecordizeshould return an instance ofActiveRecord::NullRelation.