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 …
我正在尝试解决上传非带有操作文本的图像的文件的问题,并使它们对查看帖子的用户有用。例如,如果在创建某些内容时上传了 PDF,用户应该能够下载。
\n\n现在我的图像上传和显示都很好,但如果我上传了 PDF,它会显示文件名和大小:
\n\nBla.pdf 635 KB。看起来这是不可点击的:
\n\n\n\n下面的 html 是:
\n\n<action-text-attachment sgid=\xe2\x80\x9dBAh7CEkiCGdpZAY6BkVUSSI4Z2lkOi8vdGFhYWxrLWVkZ2UvQWN0aXZlU3RvcmFnZTo6QmxvYi82OD9leHBpcmVzX2luBjsAVEkiDHB1cnBvc2UGOwBUSSIPYXR0YWNoYWJsZQY7AFRJIg9leHBpcmVzX2F0BjsAVDA= \xe2\x80\x94 9fc1638e6a6bca562cbff92f1627dfcf9e74f198" content-type=\xe2\x80\x9dapplication/pdf\xe2\x80\x9d url="http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBTUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--5da8c8c82a642f3ed3f0c158a2560a7a41b974c7/Bla.pdf" filename=\xe2\x80\x9dBla.pdf\xe2\x80\x9d filesize=\xe2\x80\x9d649993"><figure class=\xe2\x80\x9dattachment attachment \xe2\x80\x94 file attachment \xe2\x80\x94 pdf\xe2\x80\x9d>\n<figcaption class=\xe2\x80\x9dattachment__caption\xe2\x80\x9d>\n<span class=\xe2\x80\x9dattachment__name\xe2\x80\x9d>Bla.pdf</span>\n<span class=\xe2\x80\x9dattachment__size\xe2\x80\x9d>635 KB</span>\n</figcaption>\n</figure></action-text-attachment>\nRun Code Online (Sandbox Code Playgroud)\n\n有什么想法可以让我下载这个吗?
\n\n谢谢
\n我有一个现有的 Rails 项目,它是在 API 模式下创建的,不使用数据库。在尝试部署到 Heroku 时,我收到许多与 ActiveRecord 相关的错误。
-----> Detecting rake tasks
!
! Could not detect rake tasks
! ensure you can run `$ bundle exec rake -P` against your app
! and using the production group of your Gemfile.
! rake aborted!
! URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbname
...
...
/activerecord-6.0.2.1/lib/active_record/railties/databases.rake
Run Code Online (Sandbox Code Playgroud) 我在生产服务器上设置了一个典型的 Rails 6.0.2 应用程序,并且我使用 git hooks 来部署该应用程序。当我尝试运行:rails assets:precompile我收到以下错误:
rake aborted!
Sprockets::ArgumentError: link_directory argument must be a directory
/home/user/myapp/app/assets/config/manifest.js:2
Run Code Online (Sandbox Code Playgroud)
这是manifest.js 文件
//= link_tree ../images
//= link_directory ../stylesheets .css
Run Code Online (Sandbox Code Playgroud)
我不明白我做错了什么。除非我弄错了,否则 webpacker 不会取代 sprocket 吗?
我无法在控制器问题上使用匿名控制器从规范访问会话或路由路径(root_url)。
这是我的代码
module SecuredConcern
extend ActiveSupport::Concern
def logged_in?
session[:user_info].present?
end
def redirect_to_login
redirect_to login_path unless logged_in?
end
def redirect_to_home
redirect_to root_path if logged_in?
end
end
Run Code Online (Sandbox Code Playgroud)
和规格
require 'rails_helper'
describe SecuredConcern, type: :controller do
before do
class FakesController < ApplicationController
include SecuredConcern
end
end
after { Object.send :remove_const, :FakesController }
let(:object) { FakesController.new }
# let(:session) {create(:session, user_info: '')}
describe 'logged_in' do
it "should return false if user is not logged in" do
expect(object.logged_in?).to eq(false)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是跟踪:
Module::DelegationError:
ActionController::Metal#session …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails rspec-rails activesupport-concern ruby-on-rails-6
我有一个来自 GraphQL 的对象。我需要处理一个字段,然后返回一个哈希值。
为此,我最初尝试使用该to_h方法。但是当我尝试更改该字段中的值时,出现此错误:
FrozenError - 无法修改冻结的哈希值
然后我就产生了使用该方法的想法dup。这个方法似乎“解冻”了原始对象:
object.to_h.dup
Run Code Online (Sandbox Code Playgroud)
但这对于“深”领域没有帮助。对于“深层”字段,我必须使用本地dup方法。
请告诉我是否有更正确的解决方案?
显然rescue_from应该捕获异常,但这并不能按预期工作:
class ApplicationController < ActionController::Base
rescue_from ActionController::RoutingError, with: :not_found
def not_found
text: 'Not found'
end
end
Run Code Online (Sandbox Code Playgroud)
规格:
specify 'Not found' do
visit '/zzz'
expect(page.status_code).to eq 200
end
Run Code Online (Sandbox Code Playgroud)
Failure/Error: visit '/zzz'
ActionController::RoutingError:
No route matches [GET] "/zzz"
Run Code Online (Sandbox Code Playgroud)
开发环境中的行为相同。
但是,挽救其他错误(例如)RuntimeError确实按预期工作。
文档:https ://apidock.com/rails/v6.0.0/ActiveSupport/Rescuable/ClassMethods/rescue_from
导轨 6.0.2
为什么不能RoutingError与 一起使用rescue_from?RoutingError 是在中间件中引发的还是在调用控制器之前由路由器引发的?还有别的办法可以抓吗RoutingError?
假设我们有一些经典的东西
@physician.availabilities.class
=> Availability::ActiveRecord_Associations_CollectionProxy
@physician.availabilities.length
=> 72
Run Code Online (Sandbox Code Playgroud)
我们如何从该对象中随机采样记录?
例如,这看起来很有希望
@physician.availabilities.sample(5)
Run Code Online (Sandbox Code Playgroud)
除此之外
@physician.availabilities.sample(5).class
=> Array
Run Code Online (Sandbox Code Playgroud)
而我想重新训练 ActiveRecord 对象(这样我可以.save随机采样,但不能采样其他对象)
我已经设置了印象派宝石。它一直工作正常,正在更新展示次数,但在尝试获取 Films#Show 页面时突然收到以下错误。
\n\n\n“60cb104e4befe185a8b81aac9a2c5e3c”的未定义方法“cookie_value\”:字符串
\n
似乎与 session_hash 有关,但不知道如何解决这个问题。
\n有人有什么想法吗?
\n这是我的电影模型:
\n class Film < ApplicationRecord\n\n\n acts_as_votable\n is_impressionable\n\n mount_uploader :cover, ImageUploader\n mount_uploader :video, VideoUploader\n mount_uploader :trailer, VideoUploader\n\n\n\n def score\n self.get_upvotes.size - self.get_downvotes.size\n end\n\n extend FriendlyId\n friendly_id :title, use: :slugged\n\n # one film has many reviews\n has_many :reviews, dependent: :destroy\n\n validates :title, presence: true\n validates :author, presence: true\n validates :cover, presence: true\n validates :description, presence: true\n\nend\nRun Code Online (Sandbox Code Playgroud)\n电影放映页面
\n<%= render "shared/ticker" %>\n\n\n\n\n<div class="back-to-films">\n <%= …Run Code Online (Sandbox Code Playgroud) ruby-on-rails session-cookies impressions impressionist ruby-on-rails-6
有没有一种简单的方法来自定义当参数pattern: "([A-Za-z0-9\-\_]+)"不满足并且出现此消息时出现的消息:
例如
<%= f.text_field :username, pattern: "([A-Za-z0-9\-\_]+)" %>
Run Code Online (Sandbox Code Playgroud) ruby-on-rails ×10
ruby-on-rails-6 ×10
actiontext ×1
file ×1
form-for ×1
form-with ×1
impressions ×1
rspec-rails ×1
ruby ×1
webpacker ×1