【问题标题】:How to scope only the tags a user has used如何仅限定用户使用过的标签
【发布时间】:2015-06-04 11:53:02
【问题描述】:

我正在编写一个带有模型用户、记事卡、标签和标记的 Rails 4.2 应用程序(用于 m-2-m 关系)。 一个标签可以有多个记事卡,一个记事卡可以有多个标签。 卡片属于用户,标签不属于用户。 如何确定只有用户使用过的标签? 我想要一个所有标签的索引和一个用户实际使用过的标签的索引。

谢谢!

这是架构,因为我不知道如何实现 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

【问题讨论】:

  • 你能把你的代码贴一下吗,它会帮助回答
  • 完成。我在 def index_of_used_tags 上添加了架构,因为我在飞盲 #Take 所有标签,从这个用户端返回那些有卡片的标签
  • 你能列出这些类之间的所有关联吗? (在您的问题中,不是评论)。
  • 您可以在UserTaggingCard 之间建立has_many 关系。
  • current_user.cards.tags 将不起作用,因为 current_user.cards 将返回一个集合。调用 tags 会给你 NoMethodError。

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


【解决方案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 会给你用户使用过的所有标签。

【讨论】:

  • 在标签中,你能看到card_id吗?
  • card_id ? CardTag 之间的关系是多对多的。
  • 如果你想要一个用户所有卡片的id,你可以user.card_ids
  • 我的问题错了..如果您使用的是卡片,那么在卡片表中,我们必须有 tag_id 和 user_id..对吗?
  • 一个Card属于一个User所以它会有user_id,但是CardTag之间的关系是多对多的所以它不会有@ 987654333@。 tag_id 将出现在 taggings 表中。
【解决方案2】:
User.includes(:cards => :taggings).where('users.id = ?', current_user.id)

试试这个查询

【讨论】:

  • 请加一点解释。只是一个修复并不像为什么那么有趣。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 2019-08-14
  • 2022-11-19
  • 1970-01-01
  • 2020-09-01
相关资源
最近更新 更多