如何仅限定用户使用的标记

时间:2015-06-04 11:53:03

标签: ruby-on-rails ruby-on-rails-4 scope tags

我正在编写一个带有模型用户,记录卡,标记和标记的Rails 4.2应用程序(用于m-2-m关系)。 标签可以有多个记录卡,记事卡可以有多个标签。 卡属于用户,标签DOESN&T; T属于用户。 如何确定仅供用户使用的标记的范围? 我希望有一个所有标签的索引和用户实际使用的标签索引。

谢谢!

这是架构,因为我不知道如何实现where子句来索引用户使用的标签。 为了给你一个想法,我正在寻找类似的东西

def index_of_used_tags
  #Take all tags, return those that have cards from this user
end

class User < ActiveRecord::Base
  has_many :comments
  has_many :folders
  has_many :cards
end
class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :cards, through: :taggings

  validates_presence_of :name
  validates_uniqueness_of :name
end
class Folder < ActiveRecord::Base
  belongs_to :user

  validates_presence_of :name
  validates_uniqueness_of :name, scope: :user_id
end
class Card < ActiveRecord::Base
  belongs_to :user
  belongs_to :folder

  has_many :taggings
  has_many :tags, through: :taggins
end
ActiveRecord::Schema.define(version: 20150604113358) do
  enable_extension "plpgsql"
  create_table "cards", force: :cascade do |t|
    t.string   "object"
    t.text     "content"
    t.string   "source"
    t.integer  "user_id"
    t.integer  "folder_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
  add_index "cards", ["folder_id"], name: "index_cards_on_folder_id", using: :btree
  add_index "cards", ["user_id"], name: "index_cards_on_user_id", using: :btree
  create_table "comments", force: :cascade do |t|
    t.text     "content"
    t.string   "commentable_type"
    t.integer  "commentable_id"
    t.integer  "user_id"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
  end
  add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree
  create_table "folders", force: :cascade do |t|
    t.string   "name"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
  add_index "folders", ["user_id"], name: "index_folders_on_user_id", using: :btree
  create_table "taggings", force: :cascade do |t|
    t.integer  "card_id"
    t.integer  "tag_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
 add_index "taggings", ["card_id"], name: "index_taggings_on_card_id", using: :btree
 add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
 create_table "tags", force: :cascade do |t|
   t.string   "name"
   t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
 end
create_table "users", force: :cascade do |t|
  t.string   "fname"
  t.string   "lname"
  t.boolean  "admin",                  default: 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.datetime "created_at",                             null: false
  t.datetime "updated_at",                             null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_foreign_key "cards", "folders"
add_foreign_key "cards", "users"
add_foreign_key "comments", "users"
add_foreign_key "folders", "users"
add_foreign_key "taggings", "cards"
add_foreign_key "taggings", "tags" end

2 个答案:

答案 0 :(得分:1)

您可以在用户和代码

之间设置has_many through关系
class User < ActiveRecord::Base
  has_many :comments
  has_many :folders
  has_many :cards
  has_many :tags, through: :cards
end

然后user.tags会为您提供用户使用的所有标记。

答案 1 :(得分:0)

User.includes(:cards => :taggings).where('users.id = ?', current_user.id)

尝试此查询