小编Ors*_*say的帖子

为什么PG :: UniqueViolation:错误:重复键值违反了唯一约束?

我有一个模型Post,每次创建一个帖子我想要同时创建一个新的实例Moderation.

所以在post.rb中我使用回调after_save :create_moderation 然后写一个私有方法:

 ...
 include Reportable
 after_save :create_moderation

 private
 def create_moderation
    self.create_moderation!(blog: Blog.first)
 end
Run Code Online (Sandbox Code Playgroud)

但是当创建提案时,我收到此错误:

PG :: UniqueViolation:错误:重复键值违反唯一约束"moderations_reportable"DETAIL:键(reportable_type,reportable_id)=(Post,25)已存在.:INSERT INTO"moderations"("blog_id","reportable_type","reportable_id","created_at","updated_at","blog_type")VALUES($ 1,$ 2,$ 3,$ 4,$ 5,$ 6)返回"id"

在reportable.rb我有:

  has_one :moderation, as: :reportable, foreign_key: "reportable_id", foreign_type: "reportable_type", class_name: "Moderation"
Run Code Online (Sandbox Code Playgroud)

然后很少有其他方法可报告对象.

请注意,当我在控制台中运行create方法时,不会发生此问题.

编辑

  create_table "moderations", id: :serial, force: :cascade do |t|
    t.string "reportable_type", null: false
    t.string "reportable_id", null: false
    t.integer "blog_id", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "blog_type", null: false
    t.string "upstream_moderation", default: …
Run Code Online (Sandbox Code Playgroud)

ruby postgresql ruby-on-rails

11
推荐指数
3
解决办法
7614
查看次数

选择标签:如何使用参数来设置选定的值?

我有一个带有 2 个选项的选择标签:

 select_tag :type,  options_for_select("<option value = 'produit1'>Produit1</option><option value = 'Produit2'>Produit2</option>", params[:product]), id: "different_option_value_html", onchange: "this.form.submit();", include_blank: "Which products ?"
Run Code Online (Sandbox Code Playgroud)

选择其中一个选项时,表单会自动提交。我想将参数保持在第一位。我的意思是无需单击选择即可在“哪些产品”的位置可见。

ruby html-select ruby-on-rails-5 dropdown

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

如何使用 Rails 安装 Webpacker?

我正在尝试安装 webpacker 以便能够启动纱线服务器。我在 Rails 5 上并且安装了 gem。当我运行时:bundle exec rails webpacker:install 我得到:

轨道中止!不知道如何构建任务 'webpacker:install'(参见 --tasks) bin/rails:15:in require' bin/rails:15:in'(通过使用 --trace 运行任务查看完整跟踪)

该命令似乎不存在...

ruby ruby-on-rails yarnpkg webpacker

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

为什么我在 rspec 中得到的是 Active record 关系而不是 Array?

我正在运行测试Post

expect(subject.query).to eq [previous_post, post, future_comment]

但我收到这个错误:

expected: [#<Post id: 3, body: "Sed quis aut harum. Asperiores ad commodi tempore ...", ...>]


got: #<ActiveRecord::Relation [#<Post id: 3, body: "Sed quis aut harum. Asperiores a.....>]>
Run Code Online (Sandbox Code Playgroud)

它需要一个数组,但得到一个活动记录对象。我该如何解决这个问题?

这是我正在测试的代码:

      def query
        scope = Post
                .where(user_id: user.id)
                .my_scope
                .my_other_scope
                .includes(:body)

        scope = case @options[:order_by]
                when "older"
                  order_by_older(scope)
                when "most_discussed"
                  order_by_most_discussed(scope)
                else
                  order_by_older(scope)
                end

        scope
      end
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails

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

在表中创建或更新对象

我想创建一个新对象(如果不存在)。或仅更新其电子邮件(如果它已经存在于我的数据库中)。我从CSV文件导入数据。

我尝试使用find_or_create_bymethod,但是当对象已经存在时仍会在数据库中复制该对象。它不只是更新它。

人.rb

 class Person < ActiveRecord::Base
   validates :email,  uniqueness: true
   require 'csv'

   def self.import_data
     filename = File.join Rails.root, '/vendor/people.csv'

     CSV.foreach(filename, headers: true, col_sep: ',') do  |row|
       firstname, lastname, home_phone_number, mobile_phone_number,   email, address = row
       Person.find_or_create_by(firstname: row['firstname'], lastname: row['lastname'], home_phone_number: row['home_phone_number'], mobile_phone_number: row['mobile_phone_number'], email: row['email'], address: row['address'])
     end
   end
 end
Run Code Online (Sandbox Code Playgroud)

schema.rb

   create_table "people", force: :cascade do |t|
t.string   "email"
t.string   "home_phone_number"
t.string   "mobile_phone_number"
t.string   "firstname"
t.string   "lastname"
t.string   "address"
t.datetime "created_at",          null: false
t.datetime "updated_at",          null: false
end
Run Code Online (Sandbox Code Playgroud)

database ruby-on-rails ruby-on-rails-4

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

将函数插入到javascript对象中

我试图在javascript对象中插入一个函数,但我得到了

未定义

这应该是一个消息返回div.

我想第一种方式,但这是我试过的两种方式:

var errorMessage = {
     empty: function(message){"<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"
    }
 };

console.log(errorMessage.empty("Hello"));
Run Code Online (Sandbox Code Playgroud)

我也试试这种方式

function errorMessage(message){
    "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"

}

console.log(errorMessage("hello"))
Run Code Online (Sandbox Code Playgroud)

javascript function object

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