在活动记录中有一个名为find_or_create_by的方便动态属性:
Model.find_or_create_by_<attribute>(:<attribute> => "")
但是,如果我需要通过多个属性find_or_create呢?
假设我有一个模型来处理Group和Member之间的M:M关系,称为GroupMember.我可以有很多实例,其中member_id = 4,但我不想要多于一次的实例,其中member_id = 4和group_id = 7.我正在试图弄清楚是否可以这样做:
GroupMember.find_or_create(:member_id => 4, :group_id => 7)
Run Code Online (Sandbox Code Playgroud)
我意识到可能有更好的方法来处理这个问题,但我喜欢find_or_create这个想法的便利性.
activerecord many-to-many model ruby-on-rails dynamic-attributes
如何使用prawn gem在PDF表格中设置字体大小?
当我给大虾打电话时如下:
pdf = Prawn::Document.new(:page_size => 'LEGAL', :page_layout => :landscape)
pdf.table data,
:header => true,
:column_widths => widths,
:font_size => 7,
:row_colors => ["EEEEEE", "FFFFFF"]
Run Code Online (Sandbox Code Playgroud)
我得到一个NoMethodError
undefined method `font_size=' for #<Prawn::Table:0x6ce37ea4>
Run Code Online (Sandbox Code Playgroud)
当我删除":font_size => 7"时,它呈现但我得到了不合需要的字体大小.
我使用的是大虾0.12.0,ruby 1.9.3p194和Rails 3.1.9.
我有一个Rails项目,我们从Rails 5.0转换为Rails 5.1.0.当我尝试预编译资产时,我收到以下错误消息:
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
Run Code Online (Sandbox Code Playgroud)
我从互联网搜索中读到的一切都说纱线和网络包装器附带Rails 5.1.我从检查rails版本中得到以下内容.
$rails --version
Rails 5.1.0
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我也想知道发生了什么.
我正在更新我编写的生成器的代码,但直到现在一直工作正常.
当我简单地发出命令时
bundle exec rails g
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
/Users/mpierc200/projects/prototype_conflux/vendor/gems/itrc_client_files_generator-1.0.13/lib/itrc_client_files_generator.rb:6:in `<top (required)>':
uninitialized constant Rails::Generators (NameError)
Run Code Online (Sandbox Code Playgroud)
违规行是
class ItrcClientFilesGenerator < Rails::Generators::Base
Run Code Online (Sandbox Code Playgroud)
我的Rails版本是
Rails 3.1.9
Run Code Online (Sandbox Code Playgroud)
红宝石版是
ruby 1.9.3p194
Run Code Online (Sandbox Code Playgroud) 如何更换therubyracer宝石?我真的被困住了。
我们一直therubyracer在 Rails 资产管道中使用 gem。然而,它使用的依赖项已经过期并且存在安全漏洞。最新版本0.12.3为 2017 年 1 月 5 日起。
我看过mini_racergem,但我们部署到 FreeBSD,FreeBSD 支持的最新版本是0.1.142017 年 8 月 24 日开始的。我们在 macOS 上开发,我对此版本的编译mini_racer出现错误,其中一个错误是因为CreateDefaultPlatform已从 V8 中删除。
execjs 支持的运行时列表,位于https://github.com/rails/execjs列出Node.js,Google V8但链接到的页面看起来不像是我可以在 Gemfile 中替换的 gem,这些页面也没有关于如何替换的文档将它们的运行时用于 execjs。老实说,我不知道如何在我的 Rails 管道中使用更新的运行时。
我将config/environments/development.rb复制到config/environments/ci.rb.
当我使用RAILS_ENV = development运行时,资产搜索路径包括我的gem供应商目录,特别是jquery-ui-rails.但是,当我使用RAILS_ENV = ci运行时,它没有.
从控制台,Rails.application.config.assets.paths通过开发评估到以下内容
irb(main):002:0> Rails.application.config.assets.paths
=> ["/var/www/ci/conflux/app/assets/images",
"/var/www/ci/conflux/app/assets/javascripts",
"/var/www/ci/conflux/app/assets/stylesheets",
"/var/www/ci/conflux/vendor/assets/stylesheets",
"/opt/comcast/ruby1.9.3/lib/ruby/gems/1.9.1/gems/jquery-rails-1.0.19/vendor/assets/javascripts",
"/opt/comcast/ruby1.9.3/lib/ruby/gems/1.9.1/gems/jquery-ui-rails-1.0.0/vendor/assets/images",
"/opt/comcast/ruby1.9.3/lib/ruby/gems/1.9.1/gems/jquery-ui-rails-1.0.0/vendor/assets/javascripts",
"/opt/comcast/ruby1.9.3/lib/ruby/gems/1.9.1/gems/jquery-ui-rails-1.0.0/vendor/assets/stylesheets"]
Run Code Online (Sandbox Code Playgroud)
但对于ci我只得到以下内容:
irb(main):002:0> Rails.application.config.assets.paths
=> ["/var/www/ci/conflux/app/assets/images",
"/var/www/ci/conflux/app/assets/javascripts",
"/var/www/ci/conflux/app/assets/stylesheets",
"/var/www/ci/conflux/vendor/assets/stylesheets",
"/opt/comcast/ruby1.9.3/lib/ruby/gems/1.9.1/gems/jquery-rails-1.0.19/vendor/assets/javascripts"]
Run Code Online (Sandbox Code Playgroud) jquery-ui ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 asset-pipeline
所以我使用了以下内容:
git checkout --orphan newBranch
git add -A # Add all files and commit them
git commit
git branch -D master # Deletes the master branch
git branch -m master # Rename the current branch to master
Run Code Online (Sandbox Code Playgroud)
这对于 Gitrepo 提交历史记录非常有效,但是,当我使用 gitk --all 时,我仍然可以看到所有先前的提交。是否也可以将所有这些提交合并为一个?
谢谢!
我们有一个 heroku 应用程序。当我检查GC.stat早上时GC.stat[:minor_gc_count]是 51。当天晚些时候是 50。
根据我的理解,这应该是垃圾收集器做小扫的次数,所以第二天早上起来是有道理的,但为什么会减少呢?
>heroku run rails c --remote production
Running rails c on ? ... up, run.2287 (Standard-1X)
Loading production environment (Rails 5.2.2.1)
irb(main):001:0> GC.stat
=> {:count=>63, :heap_allocated_pages=>1753, :heap_sorted_length=>1753, :heap_allocatable_pages=>0, :heap_available_slots=>714528, :heap_live_slots=>713742, :heap_free_slots=>786, :heap_final_slots=>0, :heap_marked_slots=>471239, :heap_eden_pages=>1753, :heap_tomb_pages=>0, :total_allocated_pages=>1753, :total_freed_pages=>0, :total_allocated_objects=>2802530, :total_freed_objects=>2088788, :malloc_increase_bytes=>65256, :malloc_increase_bytes_limit=>32225676, :minor_gc_count=>51, :major_gc_count=>12, :remembered_wb_unprotected_objects=>4626, :remembered_wb_unprotected_objects_limit=>8538, :old_objects=>458044, :old_objects_limit=>838856, :oldmalloc_increase_bytes=>65712, :oldmalloc_increase_bytes_limit=>19737900}
irb(main):002:0> exit
**Airbrake: closed
>heroku run rails c --remote production
Running rails c on ?... up, run.7226 (Standard-1X)
Loading production environment (Rails …Run Code Online (Sandbox Code Playgroud) RuleCategory 有很多规则。我想按 RuleCategories 拥有的规则数量列出它们。
我正在使用 Rails 5.2.1,但是当我执行 group by 并尝试按 count(*) 排序时,我收到一条错误消息,因为我使用的是原始 SQL。
RuleCategory.joins(:rules).where(rules: {edit_status: [Rule::EDIT_STATUS_SYNCHED, Rule::EDIT_STATUS_EDIT]})
.group(:category).order('count(*)').limit(5).pluck(:category, :id).to_a
DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL)
called with non-attribute argument(s): "count(*)". Non-attribute arguments will be disallowed
in Rails 6.0. This method should not be called with user-provided values,
such as request parameters or model attributes.
Known-safe values can be passed by wrapping them in Arel.sql(). (called from irb_binding at (irb):2)
Run Code Online (Sandbox Code Playgroud)
如何在 order 子句中添加 count …
我正在从 Ruby 2.7 升级到 3.0,并遇到了关键字参数更改的问题。
之前我们在代码中出于某些目的使用了define_method。但是,随着关键字参数的更改,它不再能够正确处理参数数组。
class Foo
def test(a: 1, b: 2)
puts a
puts b
end
old_method = instance_method(:test)
define_method(:test) do |*args, &block|
old_method.bind(self).call(*args, &block)
end
end
Foo.new.test(a: 1)
Run Code Online (Sandbox Code Playgroud)
这将提高 wrong number of arguments (given 1, expected 0) (ArgumentError). 之前它可以在 Ruby 2.7 中运行。我们可以做些什么来让 *args 再次工作吗?
当我使用Array作为超类创建一个匿名类时,Array方法<<和[] =不会被继承.我的代码如下
class SubArray < Array
end
sa = SubArray.new
sa << "foo"
puts sa.inspect
sa_meta = Class.new(Array)
sa_meta << "foo"
puts sa_meta.inspect
Run Code Online (Sandbox Code Playgroud)
给出结果:
["foo"]
lib/so_example1.rb:9:in `<main>': undefined method `<<' for #<Class:0x6b10b8e4> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
我的红宝石版本是:
ruby 1.9.3p194 (2012-04-20 revision 35410) [i386-darwin11.4.2]
Run Code Online (Sandbox Code Playgroud)
如何用超类动态创建一个类?
ruby ×2
activerecord ×1
execjs ×1
git ×1
github ×1
heroku ×1
jquery-ui ×1
many-to-many ×1
model ×1
prawn ×1
ruby-3 ×1
scaffolding ×1
yarnpkg ×1