小编gwa*_*ton的帖子

具有2种不同连接条件的Ruby查询

我想查找具有特定LanguagesUsers记录的所有用户.目前我正在尝试查询连接表,LanguagesUsers用于用户具有2个相应的languages_id和级别的所有实例

要找到只有一个关联的位置,这有效:

User.joins(:languages_users).where(languages_users: {language_id: 2, level: 5})
Run Code Online (Sandbox Code Playgroud)

如何在查询中包含2个languages_users,它们是真的?(例如,我想language_id: 2 level: 1language_id: 3 level: 5)

我试过这个,但它返回一个错误:

User.joins(:languages_users).where(languages_users: {language_id: 2, level: 5} AND languages_users: {language_id: 1, level: 3})
Run Code Online (Sandbox Code Playgroud)

这个返回一个空数组,即使肯定有2个languages_users符合此条件的用户:

User.joins(:languages_users).where(languages_users: {language_id: 2, level: 5}).where(languages_users: {language_id: 1, level: 3})
Run Code Online (Sandbox Code Playgroud)

这是什么格式?谢谢!!

更新:LanugagesUser是语言和用户之间的连接表,具有language_id,level,user_id

gem 'sqlite3', group: :development
group :production do
    gem 'pg'
    gem 'rails_12factor'
end
Run Code Online (Sandbox Code Playgroud)

更新2:由于两个答案都提出了一个或一个陈述,我认为我没有正确地传达这个事实:

两个记录很多.例如.我想知道用户有什么语言用户(language_id:1,级别:5)和LanguagesUser(language_id:2,级别:1)

ruby mysql ruby-on-rails jointable

10
推荐指数
1
解决办法
458
查看次数

动作电缆在本地订阅,但不在heroku上订阅

我一直在尝试在网上找到的所有东西,但没有任何工作.希望一些新鲜的眼睛会看到这个问题.这是我第一次使用ActionCable,一切都在本地很好用,但是在推送到heroku时.我的日志没有显示任何actioncable订阅,如我的开发服务器:

[ActionCable] [email@email.com] MsgsChannel is streaming from msg_channel_34
Run Code Online (Sandbox Code Playgroud)

当发送消息时,我确实看到[ActionCable] Broadcasting to msg_channel_34:但它们没有附加,我猜这意味着该received方法没有被访问/调用?

我注意到heroku的日志,它说它Listening on tcp://0.0.0.0:5000在哪里作为dev在于localhost:3000.我应该以某种方式指向我的heroku app吗?

以下是相关的配置文件:

Procfile:

web: bundle exec puma -p 5000  ./config.ru  
actioncable: bundle exec puma -p 28080  cable/config.ru  
redis: redis-server  
Run Code Online (Sandbox Code Playgroud)

***感谢下面的评论,我也在努力.仍然没有工作,但我可以看到它正在收听的端口正在改变,让我相信它与配置有关?:

web: bundle exec puma -p $PORT  ./config.ru  
actioncable: bundle exec puma -p $PORT  cable/config.ru  
redis: redis-server  
Run Code Online (Sandbox Code Playgroud)

/cable/config.ru

require ::File.expand_path('../../config/environment',  __FILE__)  
Rails.application.eager_load!

ActionCable.server.config.allowed_request_origins = ["http://localhost:3000"]  
run ActionCable.server 
Run Code Online (Sandbox Code Playgroud)

配置/环境/ development.rb

config.action_cable.allowed_request_origins = ['localhost:3000']
config.action_cable.url = "ws://localhost:3000/cable"
Run Code Online (Sandbox Code Playgroud)

配置/环境/ production.rb

config.web_socket_server_url = "wss://app-name.herokuapp.com/cable" …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails heroku redis puma actioncable

10
推荐指数
1
解决办法
1440
查看次数

如何在rails控制器中调用通道方法?

我有一个订阅用户的ActionCable方法.如果启动了新的convo,我也想要将用户订阅到新频道.我无法弄清楚在控制器中调用通道方法的正确语法.

更新:问题是消息在发送时被附加到聊天框,但是当第一条消息被发送时,websocket连接尚未建立,因此它向用户查看消息是否未被发送(因为它是没有附加).

信道/ msgs_channel.rb

class MsgsChannel < ApplicationCable::Channel  
  #This function subscribes the user to all existing convos
  def subscribed
    @convos = Convo.where("sender_id = ? OR recipient_id = ?", current_user, current_user)
    @convos.each do |convo|
        stream_from "msg_channel_#{convo.id}"
    end
  end

  #This is a new function I wrote to subscribe the user to a new convo that is started during their session.
  def subscribe(convo_id)
      stream_from "msg_channel_#{convo_id}"
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的convos控制器中,创建方法,我尝试了几件事:

convos_controller.rb

def create
  @convo = Convo.create!({sender_id: @sender_id, recipient_id: @recipient_id})
  ActionCable.server.subscribe(@convo.id)
end
Run Code Online (Sandbox Code Playgroud)

ActionCable.subscribe(@convo.id)

错误: NoMethodError (undefined …

ruby ruby-on-rails ruby-on-rails-3 actioncable

10
推荐指数
1
解决办法
1785
查看次数

上传图片-Nodejs Paperclip和S3

我想将图像上传到S3并使用NodeJS保存到用户记录中,就像Rails Paperclip gem一样。

我相信这应该是一个过程,但是对于这个程序包应该如何工作,我再次感到困惑:

  1. 接收图像并通过回形针调整大小
  2. 保存或更新到S3
  3. 将文件保存到user数据库

我有一个Rails Postgres数据库,用户可以上传图像,存储在S3中,并用Paperclip gem重新格式化。存储方式如下:

irb(main):003:0> user.avatar
=> #<Paperclip::Attachment:0x000055b3e043aa50 @name=:avatar, 
@name_string="avatar", @instance=#<User id: 1, email: 
"example@gmail.com", created_at: "2016-06-11 22:52:36", 
updated_at: "2019-06-16 17:17:16", first_name: "Clarissa", 
last_name: "Jones", avatar_file_name: "two_people_talking.gif", 
avatar_content_type: "image/gif", avatar_file_size: 373197, 
avatar_updated_at: "2019-06-16 17:17:12", @options={:convert_options=>{}, 
:default_style=>:original, :default_url=>":style/missing.png", 
:escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\ 
{\}\|\\\^~%# ]/, :filename_cleaner=>nil, 
:hash_data=>":class/:attachment/:id/:style/:updated_at", 
:hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations, 
:only_process=>[], 
:path=>"/:class/:attachment/:id_partition/:style/:filename", 
:preserve_files=>false, :processors=>[:thumbnail], 
:source_file_options=>{:all=>"-auto-orient"}, :storage=>:s3, 
:styles=>{:large=>"500x500#", :medium=>"200x200#", 
:thumb=>"100x100#"}, :url=>":s3_path_url", 
:url_generator=>Paperclip::UrlGenerator, 
:use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true, 
:validate_media_type=>true, :adapter_options=> 
{:hash_digest=>Digest::MD5}, 
:check_validity_before_processing=>true, :s3_host_name=>"s3-us- 
west-2.amazonaws.com", :s3_protocol=>"https", :s3_credentials=> 
{:bucket=>"example", :access_key_id=>"REDACTED", 
:secret_access_key=>"REDACTED", 
:s3_region=>"us-west-2"}}, @post_processing=true, …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 paperclip node.js sequelize.js

8
推荐指数
1
解决办法
262
查看次数

如何在heroku上使用自定义域设置和验证cookie?

我甚至不确定这是正确的问题.

我设置ActionCable来打开一个websocket连接,它在heroku上工作得很好,如果它只是app-name.herokuapp.com但是当我尝试使用为应用程序设置的自定义域时,连接未经验证.

所以,我正在尝试在客户端上设置cookie,以便能够验证用户

通道/ application_cable/connection.rb

module ApplicationCable  
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.email
    end

    protected
      def find_verified_user # this checks whether a user is authenticated with devise
        if cookies.signed['user.id']
          verified_user = User.find_by(id: cookies.signed['user.id'])
          verified_user
        else
          reject_unauthorized_connection
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

在example.com上 - 它继续作为未经授权的连接回来.但app-name.herokuapp.com连接正常.

初始化/ warden_hooks.rb

Warden::Manager.after_set_user do |user,auth,opts|
  scope = opts[:scope]
  auth.cookies.signed["#{scope}.id"] = user.id
end
Run Code Online (Sandbox Code Playgroud)

我尝试允许来自herokuapp.com和example.com的调用:environments/production.rb

Rails.application.configure do
  config.action_cable.url = 'wss://app-name.herokuapp.com//cable'
  config.action_cable.allowed_request_origins = [ 'https://www.example.com', /http:\/\/www.example.com.*/, 'https://app-name.herokuapp.com', /http:\/\/app-name.herokuapp.com.*/ ]
end
Run Code Online (Sandbox Code Playgroud)

为什么在自定义域上未经验证?如何设置cookie变量来验证它?我对这个概念有什么误解?

谢谢! …

cookies ruby-on-rails heroku session-cookies

6
推荐指数
0
解决办法
311
查看次数

Sequelize-如何通过关联记录属性提取具有1个关联记录和顺序的记录

我有一个Convos表,其中包含许多消息。

我想要的是:提取所有重要信息和最后一条消息。订购团队last_message.created_at

models.Convos.findAll({
    include: [
        {
            model: models.Messages,
            as: "last_message",
            order: [ [ 'created_at', 'DESC' ]],
            limit: 1,
         }
    ],
    where:{
        [Op.or]: [
            {
                sender_id: req.decoded.id
            },
            {
                recipient_id: req.decoded.id
            }
        ],
    },
)}
Run Code Online (Sandbox Code Playgroud)

我最喜欢订购的是:

order: [
  [{model: models.Messages, as: 'last_message'}, 'created_at', 'DESC'],
],
Run Code Online (Sandbox Code Playgroud)

但这给出了错误:

`Unhandled rejection SequelizeDatabaseError: missing FROM-clause entry for table "last_message"`
Run Code Online (Sandbox Code Playgroud)

从这里开始,我猜想这个错误可能暗示有convos没有任何消息,从而使last_message.created_at不确定(尽管我可能完全误解了这个错误)。

因此,从那里开始,我一直试图在where语句中添加一个子句,该子句仅提取具有至少1条消息的convos。这是我尝试过的一堆东西,它们都抛出错误:

添加到哪里:

Sequelize.literal("`last_message`.`id` IS NOT NULL")


'$models.Messages.id$': { [Op.ne]: null },

Sequelize.fn("COUNT", Sequelize.col("last_message")): { [Op.gt]: 0 }
'$last_message.id$': { [Op.ne]: …
Run Code Online (Sandbox Code Playgroud)

node.js sequelize.js

6
推荐指数
1
解决办法
198
查看次数

亚马逊产品 API - 错误!AMS ScratchPad 上的 TooManyRequests

我获得了亚马逊产品 API 的批准,并且正在 ScratchPad 中进行了几次测试调用,但它不断返回错误:

Error! TooManyRequests The request was denied due to request throttling. Please verify the number of requests made per second to the Amazon Product Advertising API.

我从来没有收到过好的回应。我已经与他们的支持人员交谈了一个多星期,但没有人提供任何帮助,只是说“看起来不错,我不知道为什么你会得到这样的回应”

  • 我通过 ScratchPad 总共只打了大约 10 个电话。也许每天只有 3 次左右。
  • 过去 30 天内我有超过 3 次合格销售。 我的销售
  • 钥匙已使用超过 72 小时。甚至制作了第二对钥匙,等了三天,仍然无法使用。
  • 大约 2 周前申请了 API 使用和密钥,因此并未因 API 30 天没有销售而被禁用。

这是我在 ScratchPad 中输入的内容: 便笺

amazon-mws scratchpad aws-api-gateway paapi

6
推荐指数
1
解决办法
2534
查看次数

Rails查询,基于不相关模型的范围

我想找到一个用户的所有的convos那里是不是connect

我有一个convos表,带有sender_idrecipient_id,这两个都是对用户ID的引用

# app/models/user.rb

has_many :convos, ->(user) {
  unscope(:where).where("sender_id = :id OR recipient_id = :id", id: user.id)
}
Run Code Online (Sandbox Code Playgroud)

请注意,convo可以属于sender_id或receive_id的用户。

# app/models/convo.rb

class Convo < ApplicationRecord
  belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
  belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'

  has_many :msgs, dependent: :destroy

  validates_uniqueness_of :sender_id, :scope => :recipient_id

  scope :involving, -> (user) do
    where("convos.sender_id =? OR convos.recipient_id =?",user.id,user.id)
  end

  scope :between, -> (sender_id,recipient_id) do
    where("(convos.sender_id = ? AND convos.recipient_id =?) OR …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

5
推荐指数
0
解决办法
93
查看次数

Rails redirect_to - 我也想重新加载页面

Turbolinks 在整个应用程序中运行。我需要在某个场景中在 create 方法中重新加载页面。

这会重定向到正确的页面,但不会重新加载页面:

redirect_to convos_path


这会重定向到 convos_path,但不会重新加载页面

redirect_to convos_path, data: { no_turbolink: true } and return
Run Code Online (Sandbox Code Playgroud)

这没有任何作用

redirect_to convos_path, turbolinks: false and return

我可以在服务器日志中看到它说

Rendered convos/index.html.erb within layouts/application (12.0ms)

如何执行redirect_to,并在页面到达时重新加载页面?

redirect ruby-on-rails reload

4
推荐指数
1
解决办法
3857
查看次数

S3上的Rails + ActiveStorage:是否设置下载文件名?

有没有办法在下载时更改/设置文件名?

范例:Jon Smith上传了他的头像,文件名为4321431-small.jpg。下载后,我想将文件重命名为jon_smith__headshot.jpg

视图: <%= url_for user.headshot_file %>

url_for将从Amazon S3下载文件,但使用原始文件名。

我在这里有什么选择?

filenames ruby-on-rails amazon-s3 rails-activestorage

4
推荐指数
3
解决办法
1742
查看次数