小编tit*_*oul的帖子

在数据库中存储数组:JSON与序列化数组

我想存储一个包含3个元素的数组:帖子的最后3条评论.我知道我可以将评论表加入Post一个,但我会避免为扩展目的做这个繁重的请求.

所以我想知道存储这3个元素的最佳方法是什么,因为我想在每次发表新评论时轻松更新它们:删除最后一条评论并添加新评论.

这样做的正确方法是什么?将它存储在序列化数组或JSON对象中?

arrays json ruby-on-rails

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

NoMethodError - []的未定义方法'find_by':ActiveRecord :: Relation

我一直在关注Michael Heartl教程来创建一个跟随系统,但我有一个奇怪的错误:"未定义的方法`find_by'为[]:ActiveRecord :: Relation".我正在使用设计进行身份验证.

我的观点/users/show.html.erb看起来像这样:

.
.
.
<% if current_user.following?(@user) %>
    <%= render 'unfollow' %>
<% else %>
    <%= render 'follow' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

用户模型'models/user.rb':

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,     :trackable, :validatable

has_many :authentications
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower

    def following?(other_user)
        relationships.find_by(followed_id: other_user.id)
    end

    def follow!(other_user)
        relationships.create!(followed_id: other_user.id)
    end

    def unfollow!(other_user)
        relationships.find_by(followed_id: other_user.id).destroy …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails devise nomethoderror rails-activerecord

9
推荐指数
1
解决办法
1万
查看次数

zstd:错误70:写入错误:管道损坏(无法写入解码块)

我正在尝试通过Windows终端使用zstd v1.4.0解压缩大量压缩文件,然后“ ag”搜索:

zstd -dc -r . | ag -z -i "term"

进行时它给我以下错误:

zstd:错误70:写入错误:管道损坏(无法写入解码块)

我花了数小时寻找解决方案,尝试了该zstd命令的其他选项,但无法解决。

cmd pipe ag zstandard zstd

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

搜索 .zst 文本文件

我有很大的 zstd 压缩文本文件。

我怎样才能对它们进行快速搜索?

我可以使用 AG(The Silver Searcher)或类似的东西吗?

我试过 AG 但它不起作用,我有一个“加载失败错误”:

ag -z -i "term"
Run Code Online (Sandbox Code Playgroud)

ack ag zstd

5
推荐指数
1
解决办法
3779
查看次数

考拉 - 获得一些帖子的喜欢数量

我正在使用Koala 1.7.0rc1和新的Facebook图形api面临一个问题.我正在尝试使用此请求检索帖子的数量[object_id]/likes?summary=1.此查询适用于Facebook Graph Explorer,但我无法使用Koala访问"摘要":

likes = graph.get_object("5718732097_10151698726822098", summary: 1){|data| data['likes']}
# OR
likes = graph.get_object("5718732097_10151698726822098/likes?summary=1")
Run Code Online (Sandbox Code Playgroud)

facebook ruby-on-rails facebook-graph-api koala koala-gem

3
推荐指数
2
解决办法
1166
查看次数

Active_support/dependencies.rb:247`require':无法加载此类文件 - 2.1/pg_ext(LoadError)

我在Windows机器上使用Ruby 2.1.5,Rails 4.1.6和PostgreSQL 9.3.5.

当我尝试时bundle exec rails server,我最终得到以下错误:

DL is deprecated, please use Fiddle
c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require': cannot load such file -- 2.1/pg_ext (LoadError)
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in re...re'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependancy'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/pg-0.17.1-x86-mingw32/lib/pg.rb:10:in `rescue in <top (required)>'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/pg-0.17.1-x86-mingw32/lib/pg.rb:3:in `<top (required)>'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:76:in `require'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:72:in `each'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:72:in `block in require'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:61:in `each'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.7.7/lib/bundler/runtime.rb:61:in `require'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.7.7/lib/bundler.rb:133:in `require'
 from C:/....../config/application.rb:5:in `<top (required)>'
 from c:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:79:in `require' …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql ruby-on-rails

3
推荐指数
1
解决办法
2477
查看次数

PGError:错误:关系"用户"的列"电子邮件"已存在

我一直在开发一个关于localhost的网站,它很好.今天早上,我尝试使用命令"git push heroku master"将其推送到heroku,然后"heroku run rake db:migrate".当我尝试做第二个时,我有一个错误:

Connecting to database specified by DATABASE_URL
Migrating to DeviseCreateUsers (20130427200347)
Migrating to CreateAuthentications (20130427210108)
Migrating to AddTokenToAuth (20130427233400)
Migrating to AddNotificationToAuth (20130427234836)
Migrating to AddNotifToUser (20130428031013)
Migrating to AddDeviseToUsers (20130712103048)
==  AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:

PGError: ERROR:  column "email" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "email" character varying(255) DEFAULT '' NOT N
sql_adapter.rb:652:in `async_exec'
Run Code Online (Sandbox Code Playgroud)

我找到了一个有同样问题的人( …

migration ruby-on-rails heroku devise

2
推荐指数
1
解决办法
5302
查看次数

Sidekiq - 重新安排失败的工作

我有一个后台工作使用Sidekiq连接到我的另一个服务,如下所示:

  def perform(id)
      user = ABCClient.instance.user(id)
      ...
  end
Run Code Online (Sandbox Code Playgroud)

有时这个ABCClient已关闭,我想在这种情况下重新安排"执行"工作.像这样:

  def perform(id)
      begin
          user = ABCClient.instance.user(id)
      rescue => e
          # Reschedule job
      end
      ...
  end
Run Code Online (Sandbox Code Playgroud)

redis sidekiq

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