Consider this simple: has_many relation:
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 in which I want to create a temporary copy of the apples array and manipulate the temporary copy. First, I want to add a new item to a temporary copy as follows:
class Basket < ActiveRecord::Base has_many :apples def do_something
When I do this, I find that validate routines are called on a temporary Apple object when I try to add it to my temporary array. The whole reason I created a temporary array is to avoid all the behavior that comes with the main array, such as checking, inserting a database, etc.
However, I found brute force to avoid this problem by creating temp_array one object at a time in a for loop, as shown below. It works, but it is ugly. I am wondering if there is a more elegant way to achieve this.
class Basket < ActiveRecord::Base has_many :apples def do_something
If anyone has a better solution to this problem than what I heard, I'd love to hear it.
Thanks!
Denis source share