Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord: treating has_many list as a simple array

Consider this simple :has_many relationship:

class Basket < ActiveRecord::Base
    has_many :apples
    ...
end

class Apple < ActiveRecord::Base
    belongs_to :basket
end

Now, I have a method in the Basket class wherein I want to create a temporary copy of the 'apples' array and manipulate the temporary copy. For starters, I want to add a new element to the temporary copy as follows:

class Basket < ActiveRecord::Base
    has_many :apples

    def do_something
        #create a temporary working copy of the apples array
        temp_array = self.apples

        #create a new Apple object to insert in the temporary array
        temp_apple = Apple.new

        #add to my temporary array only
        temp_array << temp_apple

        #Problem! temp_apple.validate gets called but I don't want it to.
    end
end

When I do this, I find that the validate routines get called on the temporary Apple object when I try to add it to my temporary array. The whole reason why I've created the temporary array is to avoid all of the behavior that comes with the primary array such as validation, database insertion, etc...

That said, I did find a brute force way to avoid this problem creating the temp_array one object at a time in a for loop as shown below. This works but it's ugly. I'm wondering if there's a more elegant way of achieving this.

class Basket < ActiveRecord::Base
    has_many :apples

    def do_something
        #create a temporary working copy of the apples array
        temp_array = []
        for x in self.apples
            temp_array << x
        end

        #create a new Apple object to insert in the temporary array
        temp_apple = Apple.new

        #add to my temporary array only
        temp_array << temp_apple

        #Yippee! the temp_apple.validate routine doesn't get called this time!.
    end
end

If anybody has a nicer solution to this problem than what I have hear I'd love to hear it.

Thanks!

like image 829
Denis Avatar asked Dec 16 '22 04:12

Denis


1 Answers

The problem is that self.apples is not actually an Array - it is a Relation, that will be resolved once you apply an Array/Enumerable method to it. So, after this: temp_array = self.apples even the SQL query has not been fired.

The simple solution to force getting data, and get rid of all that Relation behavior, is just to use method all:

#create a temporary working copy of the apples array
temp_array = self.apples.all
like image 175
alony Avatar answered Jan 01 '23 07:01

alony