我需要批量更新数千条记录,我想分批处理更新.首先,我试过:
Foo.where(bar: 'bar').find_in_batches.update_all(bar: 'baz')
Run Code Online (Sandbox Code Playgroud)
...我希望生成SQL,例如:
"UPDATE foo SET bar = 'baz' where bar='bar' AND id > (whatever id is passed in by find_in_batches)"
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为find_in_batches返回一个数组,而update_all需要一个ActiveRecord关系.
这是我接下来尝试的:
Foo.where(bar: 'bar').select('id').find_in_batches do |foos|
ids = foos.map(&:id)
Foo.where(id: ids).update_all(bar: 'baz')
end
Run Code Online (Sandbox Code Playgroud)
这是有效的,但它显然运行一个选择后跟更新,而不是基于我的'where'条件的单个更新.有没有办法清理它,以便选择和更新不必是单独的查询?
我正在尝试手动创建/更新db/structure.sql文件以进行测试.我config.active_record.schema_format = :sql在application.rb中设置了" ".我想知道是否有一个命令等效于" ActiveRecord::SchemaDumper.dump"将创建或更新structure.sql.我已经尝试过structure_dump,但输出为nil:
> ActiveRecord::Base.connection.structure_dump
=> nil
Run Code Online (Sandbox Code Playgroud)
我也尝试将文件传递给该方法:
> File.open( File.join(Rails.root, 'db/structure.sql')) { |f| ActiveRecord::Base.connection.structure_dump() }
=> nil
Run Code Online (Sandbox Code Playgroud)
到目前为止,我能够生成或更新db/structure.sql的唯一方法是运行rake db:migrate.任何替代品?或者我在尝试运行ActiveRecord :: Base.connection.structure_dump时遗漏了什么?
我正在使用IO对象(一些STDOUT输出文本),我正在尝试将其转换为字符串,以便我可以进行一些文本处理.我想做这样的事情:
my_io_object = $stdout
#=> #<IO:<STDOUT>>
my_io_object.puts('hi') #note: I know how to make 'hi' into a string, but this is a simplified example
#=>hi
my_io_object.to_s
Run Code Online (Sandbox Code Playgroud)
我尝试了一些事情并得到了一些错误:
my_io_object.read
#=> IOError: not opened for reading
my_io_object.open
#=> NoMethodError: private method `open' called for #<IO:<STDOUT>>
IO.read(my_io_object)
#=> TypeError: can't convert IO into String
Run Code Online (Sandbox Code Playgroud)
我已经阅读了IO类方法,我无法弄清楚如何操作该对象中的数据.有什么建议?
我正在尝试访问Postgres 9.2数据库中列的默认值.通过使用原始SQL,我可以验证列默认为"users_next_id()":
> db = ActiveRecord::Base.connection
> db.execute("SELECT table_name,column_name,column_default
FROM information_schema.columns
WHERE table_name = 'users' and column_name ='id'").first
=> {"table_name"=>"users",
"column_name"=>"id",
"column_default"=>"users_next_id()"}
Run Code Online (Sandbox Code Playgroud)
但是当我使用AR的'columns'方法时,默认值似乎是nil:
[26] pry(main)> db.columns('users')[0]=> #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007feb397ba6e8
@coder=nil,
@default=nil,
@limit=8,
@name="id",
@null=false,
@precision=nil,
@primary=nil,
@scale=nil,
@sql_type="bigint",
@type=:integer>
Run Code Online (Sandbox Code Playgroud)
这不会导致任何问题(除了让我困惑),但这是预期的行为吗?我对"列"方法做出了错误的假设吗?
我一直在查看1.9.3 的Ruby标准库文档.我想知道如何找出我需要明确要求的库.
例如,我被告知我需要明确要求json库,但不是rubygems,尽管它们都出现在标准库列表中.
还有另一个地方我可以查找有关要求的信息,甚至是我可以运行的pry/irb命令来查看已经"需要"的内容吗?
我正在尝试在我的Mac上安装charlock_holmes(10.7.5,ruby-1.9.3-p392),我遇到以下错误:
$gem install charlock_holmes -v '0.6.9.4'
Building native extensions.
This could take a while...
ERROR: Error installing charlock_holmes:
ERROR: Failed to build gem native extension.
/Users/mthompson/.rvm/rubies/ruby-1.9.3-p392/bin/ruby extconf.rb
checking for main() in -licui18n... no
checking for main() in -licui18n... no
***************************************************************************************
*********** icu required (brew install icu4c or apt-get install libicu-dev) ***********
***************************************************************************************
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may …Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习Rails的安全性,我想知道如何在允许用户将CSV文件上传到我们的数据库的同时避免安全性问题。我们正在使用Postgres的“从stdin复制”功能将CSV中的数据上传到临时表中,然后将其用于将upserts插入另一个表中。这是基本代码(由于这篇文章):
conn = ActiveRecord::Base.connection_pool.checkout
raw = conn.raw_connection
raw.exec("COPY temp_table (col1, col2) FROM STDIN DELIMITER '|'")
# read column values from the CSV line by line in the following format:
# attributes = {column_1: 'column 1 data', column_2: 'column 2 data'}
# line = "#{attributes.values.join('|')}\n"
rc.put_copy_data line
# wrap up copy process & insert into & update primary table
Run Code Online (Sandbox Code Playgroud)
我想知道我能还是应该做的消毒列值。我们正在使用Rails 3.2和Postgres 9.2。
安装 github-markdown 0.6.9 gem 时,出现以下错误:
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/Moth/.rvm/gems/ruby-2.6.6@searchgov-rails42/gems/github-markdown-0.6.9/ext/markdown
/Users/Moth/.rvm/rubies/ruby-2.6.6/bin/ruby -I /Users/Moth/.rvm/rubies/ruby-2.6.6/lib/ruby/site_ruby/2.6.0 -r
./siteconf20210820-81082-7n7xnz.rb extconf.rb
creating Makefile
current directory: /Users/Moth/.rvm/gems/ruby-2.6.6@searchgov-rails42/gems/github-markdown-0.6.9/ext/markdown
make "DESTDIR=" clean
current directory: /Users/Moth/.rvm/gems/ruby-2.6.6@searchgov-rails42/gems/github-markdown-0.6.9/ext/markdown
make "DESTDIR="
compiling autolink.c
compiling buffer.c
compiling gh-markdown.c
gh-markdown.c:56:29: error: implicitly declaring library function 'isspace' with type 'int (int)'
[-Werror,-Wimplicit-function-declaration]
while (i < lang->size && !isspace(lang->data[i]))
^
gh-markdown.c:56:29: note: include the header <ctype.h> or explicitly provide a declaration for 'isspace'
gh-markdown.c:60:14: warning: assigning to 'const char *' …Run Code Online (Sandbox Code Playgroud)