防止Rails多次访问数据库(preventing Rails hitting database so many times)

编程入门 行业动态 更新时间:2024-10-17 11:26:38
防止Rails多次访问数据库(preventing Rails hitting database so many times)

当创建新的unit_users记录(将单元与用户关联)时,在保存用户时,在使用帐户ID更新单元时,以下方法会在保存帐户时命中数据库。 这至少是它击中数据库的4倍:

def activate_new_account(assign_units = []) account = Account.new( :name => self.name, :email => self.email, :phone => self.phone, :street_address => self.street_address, :city => self.city, :state => self.state, :postal_code => self.postal_code, :country => self.country) errors.clear error_msgs = [] transaction do if account.valid? account.save user = User.new(:name => self.name, :email => self.email, :password => self.password, :password_confirmation => self.password_confirmation, :phone => self.phone, :address => formatted_address, :role_id => self.user_role_id, :account_id => account.id) if user.valid? user.save if units_for_account begin units = Unit.find(units_for_account.split(" ")) units.each do |unit| #hitting database twice unit.update_attributes account_id: account.id unit.users << user end rescue ActiveRecord::RecordNotFound error_msgs << "Couldn't find all Units with serial numbers: #{units_for_account.split(' ')}" rescue ActiveRecord::RecordInvalid => invalid error_msgs << invalid.record.errors end end else account.destroy error_msgs << user.errors.full_messages end else error_msgs << account.errors.full_messages end end if error_msgs.size > 0 error_msgs.each do |error| errors.add :base, error end return false end return true end

有没有更多的Railsy方法来做到这一点,而不是如此多地访问数据库?

This below method hits the database when saving an account, when saving a user, when updating the unit with the account id, when create a new unit_users record (associating unit with a user). So that's at least 4 times it hits the database:

def activate_new_account(assign_units = []) account = Account.new( :name => self.name, :email => self.email, :phone => self.phone, :street_address => self.street_address, :city => self.city, :state => self.state, :postal_code => self.postal_code, :country => self.country) errors.clear error_msgs = [] transaction do if account.valid? account.save user = User.new(:name => self.name, :email => self.email, :password => self.password, :password_confirmation => self.password_confirmation, :phone => self.phone, :address => formatted_address, :role_id => self.user_role_id, :account_id => account.id) if user.valid? user.save if units_for_account begin units = Unit.find(units_for_account.split(" ")) units.each do |unit| #hitting database twice unit.update_attributes account_id: account.id unit.users << user end rescue ActiveRecord::RecordNotFound error_msgs << "Couldn't find all Units with serial numbers: #{units_for_account.split(' ')}" rescue ActiveRecord::RecordInvalid => invalid error_msgs << invalid.record.errors end end else account.destroy error_msgs << user.errors.full_messages end else error_msgs << account.errors.full_messages end end if error_msgs.size > 0 error_msgs.each do |error| errors.add :base, error end return false end return true end

Is there a more Railsy way to do this without hitting database so much?

最满意答案

通过使用validates_associated ,有更多Rails方法可以完成激活。 但它没有更少地访问数据库。 您有四个表来更新或添加行,因此您需要四个DB语句。

There is a much more Rails way to accomplish the activation, by using validates_associated. But it does'n access the database less. You have four tables to update or add rows, so you need four DB statements.

更多推荐

本文发布于:2023-07-22 09:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   preventing   Rails   times   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!