Ruby on rails 避免复制ActiveRecord::Base.connection\u pool.with\u connection
我有一些函数使用ActiveRecord来保存一些数据Ruby on rails 避免复制ActiveRecord::Base.connection\u pool.with\u connection,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一些函数使用ActiveRecord来保存一些数据 def create_location(user_location) ActiveRecord::Base.connection_pool.with_connection do loc = Location.new(user_location.attrs) loc.save! end end 如何避免代码重复 因为现在在每个方法中,我都必须将所有内容包装在ActiveRecord::Base.connection\
def create_location(user_location)
ActiveRecord::Base.connection_pool.with_connection do
loc = Location.new(user_location.attrs)
loc.save!
end
end
如何避免代码重复
因为现在在每个方法中,我都必须将所有内容包装在
ActiveRecord::Base.connection\u pool.with\u connection
中,您的问题是什么?如何避免重复?您是否考虑过将重复的代码移动到助手函数/方法中的方法?哪个接受块作为参数?