在我的应用程序中实现Web套接字我很困惑哪个gem更好.我发现了很多不同的机会,但有些机会难以区分.
最后,我选择了Action Cable(Rails 5本机部分)和Faye(之前出现并且变得非常受欢迎).
但现在我被卡住了 - Action Cable和Faye一样吗?哪些是差异(如果有的话)?
假设我db0
在本地计算机和db1
远程服务器上都有。我只是想了解db1
最新情况db0
。
my_table
假设我向in添加了几行(数千行)db0
,现在我想将其插入到my_table
in中db1
。
关于 pg_dump/pg_restore 的所有食谱和文档都提到了表的完全恢复,但是,我不需要(也不想)删除并my_table
从头开始恢复。
是否有任何清晰且简单的方法来制作文件,scp
将其发送到服务器并从中pg_restore
取出文件?
只需两个命令:pg_dump 和 pg_restore。因为我花了大约两个小时查看文档和演练,发现了新的错误,并且变得越来越困惑。
Situation: I have a small chat app with two users in each room. Lets call'em Sender and Receiver.
I'd like to make 'read/unread messages'.
To determine if Sender's message is currently being read he needs to know if Receiver is currently subscribed to the channel.
I'm trying to look through subscriptions:
# I can retrieve subscriptions from Redis:
redis_record = Redis.new.pubsub("channels", "action_cable/*")
# I can make such id
key = Base64.encode64("gid://test-app/User/1").tr('=', '').tr('actioncable/', '').chomp
Run Code Online (Sandbox Code Playgroud)
But I don't know how to …
我有一个特定的问题所以我会把它翻译成更容易理解和清晰的例子.
所以我们有'Country'和'Image'模型.一个国家有它的旗帜和它的手臂.
这意味着我们必须将Country to Image连接2次.我尝试转换Rails指南的配方'连接到自己',然而,我总是得到一个例外:"图像预期,得到字符串".
*Country model
class Country < ApplicationRecord
has_one :flag, class_name: 'Image', foreign_key: 'id'
has_one :arm, class_name: 'Image', foreign_key: 'id'
end
*Image model
class Image < ApplicationRecord
belongs_to :flag, class_name: 'Country'
belongs_to :arm, class_name: 'Country'
end
Run Code Online (Sandbox Code Playgroud) 我们有一个数组,说:
my_arr = [121, 23, 46, 91, 38, 140]
Run Code Online (Sandbox Code Playgroud)
我们还有索引数组:
indxs = [1, 2, 4]
Run Code Online (Sandbox Code Playgroud)
我知道values_at
方法,但是它不接受数组,但是它可以接受多个值,证明:https : //ruby-doc.org/core-2.5.1/Array.html#method-i-values_at
也许您可以给我一个如何处理的线索values_at
。提前致谢!
编辑:我们有2个答案,所以II做了一个基准测试(1000次迭代,array.size = 20,indexs.size = 4)
user system total real
values_at 0.004502 0.000000 0.004502 ( 0.004476)
map[i] 0.005878 0.000000 0.005878 ( 0.006968)
Run Code Online (Sandbox Code Playgroud) 我的情况非常复杂,这就是为什么我用简单数字替换原始数据的原因。因此,请不要注意非常简单的数据和条件是否“愚蠢”的情况。这只是一个例子。另外,如果有错,请忽略错别字-原始代码没有错字。
我有一些像这样的哈希值数组 my_hsh = {"numbers" => [1, 2, 3, 4], "n_count" => 4}
我需要做什么:
my_arr_nochange
,the_hash["numbers"]
,my_arr_updt
。所以代码:
the_hash = {"numbers" => []}
my_arr_updt = []
my_arr_nochange = []
array_of_hashes.each do |my_hsh|
if my_hsh["n_count"] == 4
my_arr_nochange << my_hsh
updated_hsh = my_hsh
my_hsh["numbers"].each do |num|
if num == 2
the_hash["numbers"] += [ num ]
updated_hsh["numbers"] -= [ num ]
end
end
my_arr_updt << updated_hsh
end
end
return the_hash, my_arr_updt, my_arr_nochange
Run Code Online (Sandbox Code Playgroud)
问题正在my_arr_nochange
被修改,所以我没有获取旧状态的my_hsh而是获取了新状态。喜欢: …
今天我尝试使用一个很好的回调:after_commit在对象写入数据库时触发,但是,我收到了来自Rails的错误消息:
ActionController::RoutingError (undefined method `after_commit' for ImagesController:Class
Did you mean? after_action):
Run Code Online (Sandbox Code Playgroud)
嗯,这很令人尴尬!似乎这个回调被弃用了!通过搜索,我尝试使用:after_create_commit,它给了我同样的错误.
第三步是尝试:after_action.这里提出了一个问题: 如何使其工作方式与:after_commit?
我已经尝试过apidock.com - 它真的很小!我也尝试过api.rubyonrails.org - 它是关于块的,但我不是一个了解它的红宝石忍者.所以我真的很感激你是否可以在它上面洒一些光!
ImagesController:
class ImagesController < ApplicationController
after_create_commit :take_image_to_album
def take_image_to_album
if check_album
add_inner_to_album(@image)
end
end
def create
@image = Image.create(image_params.merge(:user_id => current_user.id)
respond_to do |format|
unless @image.save
format.html { render :show, notice: "Error!" }
format.json { render json: @image.errors, status: :unprocessable_entity }
else
format.html
format.json { render :show, status: :ok, location: @image }
end
end
end
...
def add_inner_to_album(image)
contents …
Run Code Online (Sandbox Code Playgroud) 我有一个像:
my_hash = {"one"=>{"two"=>{"three"=>"four"}}}
Run Code Online (Sandbox Code Playgroud)
我想做:
my_hash.dig("one", "two")
=> {"three"=>"four"}
Run Code Online (Sandbox Code Playgroud)
每次都对参数进行硬编码是很荒谬的,很明显使用如下变量:
my_var = "one", "two"
Run Code Online (Sandbox Code Playgroud)
不幸的是,输出根本不是很好:
my_hash.dig(my_var)
=> nil
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用,我该怎么做?
ruby ×4
actioncable ×2
arrays ×1
callback ×1
controller ×1
faye ×1
hash ×1
methods ×1
parameters ×1
pg-dump ×1
pg-restore ×1
postgresql ×1
variables ×1
websocket ×1