ruby

How can I add a function to FactoryBot globally?

落爺英雄遲暮 提交于 2021-01-28 11:00:51
问题 I have a class that extends FactoryBot to include functionality to copy Rails' .first_or_create . module FactoryBotFirstOrCreate def first(type, args) klass = type.to_s.camelize.constantize conditions = args.first.is_a?(Symbol) ? args[1] : args[0] if !conditions.empty? && conditions.is_a?(Hash) klass.where(conditions).first end end def first_or_create(type, *args) first(type, args) || create(type, *args) end def first_or_build(type, *args) first(type, args) || build(type, *args) end end I can

Mongoid delete many with limit

倾然丶 夕夏残阳落幕 提交于 2021-01-28 10:47:39
问题 Using mongoid, how to delete the first 10000 documents where the error_message field is: Error: not found . Native mongo queries will be happily accepted 回答1: MongoDB support limit on delete. The delete command removes documents from a collection. A single delete command can contain multiple delete specifications { delete: <collection>, deletes: [ { q : <query>, limit : <integer>, collation: <document> }, { q : <query>, limit : <integer>, collation: <document> }, { q : <query>, limit :

Mongoid delete many with limit

╄→гoц情女王★ 提交于 2021-01-28 10:42:12
问题 Using mongoid, how to delete the first 10000 documents where the error_message field is: Error: not found . Native mongo queries will be happily accepted 回答1: MongoDB support limit on delete. The delete command removes documents from a collection. A single delete command can contain multiple delete specifications { delete: <collection>, deletes: [ { q : <query>, limit : <integer>, collation: <document> }, { q : <query>, limit : <integer>, collation: <document> }, { q : <query>, limit :

How to check if rake task was finished?

岁酱吖の 提交于 2021-01-28 09:24:11
问题 This is my first experience with RoR and I using the rspec tool to test my controller. But inside the methods, I using rake tasks to create asynchronous solutions like this: def scan_all_urls system 'rake scan_all_urls &' end And my rake task is this: task :scan_all_urls => :environment do crawler = Crawler.new urls = Url.all crawler.scan_urls(urls) end But my issue is, at the rspec test: it 'should scan all url' do params = {} get 'scan_all_urls', params: params, format: :json end How I

Why won't my simple Connection Pool execute these simple put statements?

爷,独闯天下 提交于 2021-01-28 09:15:27
问题 I posted a question regarding how to effectively manage threads here How do I properly use Threads to connect ping a url? I got some great recommendations and tips regarding pools, thread safety, and some libraries and gems to use. I'm trying to execute one of the recommendations listed by using concurrent-ruby to create a thread/connction pool to execute some threads. In a simple ruby file I have the following code: pool = Concurrent::FixedThreadPool.new(5) pool.post do puts 'hello' end As

How to fix a “value too long for type character varying(255)” error

最后都变了- 提交于 2021-01-28 09:11:36
问题 I'm trying to save a file so that I can upload it to stripe using CarrierWave, but I'm getting the error: ERROR: value too long for type character varying(255) and don't understand why as I followed the CarrierWave usage guide on GitHub. This is my application: class SplitterStripeServer < Sinatra::Base CarrierWave.configure do |config| config.root = File.dirname(__FILE__) + "/public" end post "/" do img = Image.new img.file = params[:file] #carrierwave will upload the file automatically img

Ruby replace only exact matching string with another string

拈花ヽ惹草 提交于 2021-01-28 07:36:40
问题 How can I solve following problem? I have a html string like following one: <p>aaa, b aa aaaaa?<br/>Next possible text b bb aa b.</p> Now I'd like to replace for example only "aaa" with "<div class='special'>aaa</div>" new string after replace: <p><div class='special'>aaa</div>, b aa aaaaa?<br/>Next possible text b bb aa b.</p> So I´d like a generic replacer, which only replace an exact matching string. "aaa" was just an example. It could also be "bb" or "two words" (=> two words, so text

bundle exec not working in Windows

风流意气都作罢 提交于 2021-01-28 07:35:58
问题 I am following the Redmine Install Tutorial on step 5 it says Step 5 - Session store secret generation This step generates a random key used by Rails to encode cookies storing session data thus preventing their tampering. Generating a new secret token invalidates all existing sessions after restart. bundle exec rake generate_secret_token I'm on Windows 10. When I try to do bundle exec in CMD, I get cannot find the path specified. For whatever reason I cannot find any reference to this problem

RSpec controller for nested resources, before_action

十年热恋 提交于 2021-01-28 07:10:26
问题 Let say I have setup like this: config/routes.rb resources :graves do resources :candles end app/models/grave.rb has_many :candles, dependent: :destroy app/models/candles.rb belongs_to :grave app/controllers/candles_controller.rb before_action :get_grave def create @candle = @grave.candles.new(candle_params) respond_to do |format| if @candle.save format.html { redirect_to @grave, notice: 'Candle was successfully created.' } format.json { } else format.html { render :new } format.json { render

Find the longest substring in a string

让人想犯罪 __ 提交于 2021-01-28 06:55:46
问题 I would like to find the longest sequence of repeated characters in a string. ex: "aabbccc" #=> ccc "aabbbddccdddd" #=> dddd etc In the first example, ccc is the longest sequence because c is repeated 3 times. In the second example, dddd is the longest sequence because d is repeated 4 times. It should be something like this: b = [] a.scan(/(.)(.)(.)/) do |x,y,z| b<<x<<y<<z if x==y && y==z end but with some flags to keep the count of repeating, I guess 回答1: This should work: string = 'aabbccc'