Tag: ruby on rails 5

Rails 5 db migration:如何修复ActiveRecord :: ConcurrentMigrationError

之前的rake db:rollback停滞不前。 现在,在尝试新迁移时,我们收到以下错误: rake aborted! ActiveRecord::ConcurrentMigrationError: Cannot run migrations because another migration process is currently running. /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord- 5.1.4/lib/active_record/migration.rb:1315:in `with_advisory_lock’ /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:1148:in `migrate’ /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:1007:in `up’ /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/migration.rb:985:in `migrate’ /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:171:in `migrate’ /home/me/.rvm/gems/ruby-2.4.1@global/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:58:in `block (2 levels) in ‘ /home/me/.rvm/gems/ruby-2.4.1/gems/rake-12.1.0/exe/rake:27:in `’ /home/me/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `eval’ /home/me/.rvm/gems/ruby-2.4.1/bin/ruby_executable_hooks:15:in `’ Tasks: TOP => db:migrate (See full trace by running task with –trace) 我们正在使用Postresql

使用Rails,如何查询数据库以获取总数并按周分组?

给出一个像这样的表格: 日志: id, user_id, points, created_at 使用Rails,我如何通过user_id查询数据库,然后通过created_at逐周查询GROUPED,然后我可以使用以下内容: WEEK of X/XX/XXXX – 3011 total points WEEK of X/XX/XXXX – 320 total points WEEK of X/XX/XXXX – 31 total points WEEK of X/XX/XXXX – 30330 total points 谢谢

validationActionCable连接

我找到了一个很棒的ActionCablegem,这是SPA的一个很好的解决方案。 我想只发送html , css和js资产,所有其他连接都将通过ActionCable实现。 交换字符串或整数并不困难,但我如何通过ActionCable登录?

在rails app目录中将文件夹作为模块常量加载的方法

所以有一个rails 5项目,并希望加载这样的目录 /app /services /user foo.rb 作为常量:: Services :: User :: Foo 有没有人有过使用rails自动加载路径以这种方式加载常量的经验? foo.rb module Services module User class Foo end end end 解 将其添加到您的application.rb文件中 config.autoload_paths << Rails.root.join('app') 请参阅此处有关自动加载的讨论 https://github.com/rails/rails/issues/14382#issuecomment-37763348 https://github.com/trailblazer/trailblazer/issues/89#issuecomment-149367035

如何获取Active Storage映像的URL

我想通过api获取带有附加图像的记录列表作为链接或文件。 我有一个简单的模型: class Category < ApplicationRecord has_one_attached :image validates :name, presence: true, uniqueness: true end 接下来的行动: def index @categories = Category.all.with_attached_image render json: @categories.to_json(include: { image_attachment: { include: :blob } }) end 这是我获得图像对象的唯一方法。 我看到下一个结果: {“id”:4,”name”:”Cat1″,”description”:””}, {“id”:1,”name”:”Cat2″,”description”:””,”image_attachment”: {“id”:8,”name”:”image”,”record_type”:”Category”,”record_id”:1,”blob_id”:8,”created_at”:”2018-06-09T13:45:40.512Z”,”blob”: {“id”:8,”key”:”3upLhH4vGxZEhhf3TaAjDiCW”,”filename”:”Screen Shot 2018-06-09 at 20.43.24.png”,”content_type”:”image/png”,”metadata”: {“identified”:true,”width”:424,”height”:361,”analyzed”:true},”byte_size”:337347,”checksum”:”Y58zbYUVOlZRadx81wxOJA==”,”created_at”:”2018-06-09T13:45:40.482Z”}}}, … 我可以在这里看到文件名。 但是文件存在于不同的文件夹中,对我而言,它似乎不是一种获取和链接到文件的便捷方式。 我找不到任何关于此的信息。 更新 根据iGian解决方案,我的代码变为: def index @categories = Category.all.with_attached_image render […]

如何在rails中实现复合主键

我有一个User模型,如下所示: class User < ApplicationRecord belongs_to :organization belongs_to :department end 数据库中的users表具有两个外键organization_id和department_id 。 如何将这两列作为复合主键? 到目前为止,我在网上看过两种方法: 选项1 使用composite_primary_keys gem 选项2 使用以下内容为这两列中的每一列添加索引: add_index :users, [:organization_id, :department_id], unique: true 我的问题 唯一标识users表中行必须具有department_id和organization_id以便唯一标识的行的最佳方法是什么? 索引两列并简单地将每列作为表的主键之间的区别是什么? 谢谢!

对于将使用邮件程序参数的ActionMailer的Rails before_action

假设我有一个邮件发送不同的电子邮件,但预计将使用相同的参数调用。 我想为所有邮件程序操作处理这些参数。 因此,调用before_action将读取发送到邮件程序方法的参数 /mailers/my_mailer.rb class MyMailer < ApplicationMailer before_filter do |c| # c.prepare_mail # Will fail, because I need to pass `same_param` arguments # # I want to send the original arguments # c.prepare_mail(same_param) # How do I get `same_param` here ? end def action1(same_param) # email view is going to use @to, @from, @context […]

使用{:locale => ,:formats => 缺少模板布局/邮件程序,

我正在关注michael harlt rails教程,但是我收到了这个错误 缺少模板布局/邮件使用{:locale => [:en],:formats => [:html],:variants => [],:handlers => [:raw,:erb,:html,:builder,:ruby,:咖啡,:jbuilder]}。 搜索:*“/ home / ubuntu / workspace / app / views” 预览帐户激活时 这是我的user_mailer.rb class UserMailer < ApplicationMailer def account_activation(user) @user = user mail to: user.email, subject: "Account activation" end def password_reset @greeting = "Hi" mail to: "to@example.org" end end 并且错误突出显示的行 mail to: user.email, subject: […]

ActionController :: RoutingError:未初始化的常量Api :: V1 :: ApiController

我有用于控制用户任务的Rails 5 API项目,我有以下错误,但并不总是对于相同的控制器和路由。 ActionController::RoutingError: uninitialized constant Api::V1::ApiController 我向您描述了一些我的项目,以更详细地解释错误。 应用结构 路线 scope module: ‘api’ do namespace :v1 do # => Login routes scope module: ‘login’ do match ‘login’, to: ‘sessions#login’, as: ‘login’, via: :post end # => Team routes scope module: ‘team’ do # => no admin routes resources :tasks, except: [:index] do collection do match […]

Rails 5 SQL注入

我已经在一段时间内阅读了关于各种SO线程,指南等的内容……但所有答案都是相互矛盾和矛盾的。 似乎有许多类似的方法,很多答案都说使用不同的方法。 sanitize sanitize_conditions sanitize_sql sanitize_sql_array sanitize_sql_for_assignment sanitize_sql_for_conditions sanitize_sql_hash sanitize_sql_hash_for_assignment sanitize_sql_hash_for_conditions sanitize_sql_like 我正在尝试编写一个“原始查询”适配器,它允许我运行原始的Postgres查询,但允许我插入来自危险用户输入的我自己的参数。 我不能在这几个实例中使用AR,因为我正在进行复杂的纬度/经度计算,聚合函数,复杂子查询等。 到目前为止,我尝试了两种方法: 方法1 对于这种方法,我不知道sanitize是否是上述的最佳选择,或者它是否适用于100%的情况……(我只使用Postgres) class RawQuery def exec(prepared, *params) prepared = query.dup params.flatten.each_with_index do |p, i| prepared.gsub!(“$#{i + 1}”, ActiveRecord::Base.sanitize(p)) end ActiveRecord::Base.connection.exec_query(prepared) end end 琐碎的用法示例(当然通常不会这么简单,或者我只会使用AR): RawQuery.new.exec(‘SELECT * FROM users WHERE name = $1’, params[:name]) 此外,似乎sanitize代表quote 。 但根据这篇SOpost说它只是用单引号包装东西是不安全的…所以我不知道。 方法2 我不确定这是否同样安全,但它似乎使用了一个实际的PG准备function(我认为它是100%安全的)。 唯一的问题是rails不会将其打印到控制台,也不包括SQL执行时间(这会破坏我的分析工具)。 class RawQuery […]