从Rails - ActiveRecord向表添加约束

时间:2015-10-19 00:16:38

标签: ruby-on-rails activerecord migration rails-activerecord

我在Rails中有两个相关的模型。用户和帖子。

虽然添加了一个布尔值列,但是“阿凡达”#39;对于posts表,我想为它所属的用户创建一个唯一的真值。

换句话说,用户只能拥有一个“阿凡达”和“阿凡达”。发布值为true。这是因为这些帖子(图片)可以用作用户头像。

帖子已经属于用户,用户有很多帖子,所以我想设置一个约束,即用户只有一个帖子,其值为Avatar。此外,如果上传/选择了新的头像,则此操作可以取消并伪造任何以前的头像。

到目前为止,这是我的迁移文件:

add_avatar_to_user_profiles.rb

class AddAvatarToUserProfiles < ActiveRecord::Migration
  def change
    add_column :posts, :avatar, :boolean, default: false
  end
end

schema.rb

ActiveRecord::Schema.define(version: 20151018160617) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "comments", force: :cascade do |t|
    t.text     "comment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "post_id"
    t.integer  "user_id"
  end

  add_index "comments", ["post_id"], name: "index_comments_on_post_id", using: :btree
  add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree

  create_table "posts", force: :cascade do |t|
    t.string   "caption"
    t.integer  "likes"
    t.datetime "created_at",         null: false
    t.datetime "updated_at",         null: false
    t.integer  "user_id"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  add_index "posts", ["user_id"], name: "index_posts_on_user_id", using: :btree

  create_table "sessions", force: :cascade do |t|
    t.string   "session_id", null: false
    t.text     "data"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "sessions", ["session_id"], name: "index_sessions_on_session_id", unique: true, using: :btree
  add_index "sessions", ["updated_at"], name: "index_sessions_on_updated_at", using: :btree

  create_table "users", force: :cascade do |t|
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.string   "provider"
    t.string   "uid"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  add_index "users", ["provider"], name: "index_users_on_provider", using: :btree
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  add_index "users", ["uid"], name: "index_users_on_uid", using: :btree

  add_foreign_key "comments", "posts"
  add_foreign_key "comments", "users"
  add_foreign_key "posts", "users"
end

希望以上内容足够有意义,我搜索文档无济于事,我认为解决方案应该足够简单。

1 个答案:

答案 0 :(得分:2)

您可以在Post模型中添加自定义验证器,如下所示:

# post.rb
validate :unique_post_avatar

private

def unique_post_avatar
  if self.user.posts.select { |p| p.avatar == true }.count > 1
    errors.add(:avatar, "only one post avatar for this user can be true")
  end
end

基本上选择了头像为真的用户帖子。如果此计数大于1,则会抛出验证错误。我认为这就是你要找的东西: - )