加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ActiveRecord:将has_many列表视为一个简单的

发布时间:2020-12-17 04:04:39 所属栏目:百科 来源:网络整理
导读:考虑一下这个简单的:has_many关系: class Basket ActiveRecord::Base has_many :apples ...endclass Apple ActiveRecord::Base belongs_to :basketend 现在,我在Basket类中有一个方法,其中我想创建’apples’数组的临时副本并操作临时副本.对于初学者,我想
考虑一下这个简单的:has_many关系:

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

class Apple < ActiveRecord::Base
    belongs_to :basket
end

现在,我在Basket类中有一个方法,其中我想创建’apples’数组的临时副本并操作临时副本.对于初学者,我想在临时副本中添加一个新元素,如下所示:

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

当我这样做时,我发现当我尝试将它添加到临时数组时,会在临时Apple对象上调用validate例程.我创建临时数组的全部原因是为了避免主数组带来的所有行为,例如验证,数据库插入等……

也就是说,我确实找到了一种蛮力的方法来避免这个问题,在for循环中一次创建一个temp_array对象,如下所示.这有效,但很难看.我想知道是否有一种更优雅的方式来实现这一目标.

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

如果有人比我听到的更能解决这个问题,我很乐意听到.

谢谢!

解决方法

问题是self.apples实际上不是一个Array – 它是一个Relation,一旦你对它应用Array / Enumerable方法就会解决它.所以,在此之后:temp_array = self.apples甚至没有触发SQL查询.

强制获取数据并摆脱所有Relation行为的简单解决方案就是使用all方法:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读