ruby on rails - How to scope only the tags a user has used -
i writing rails 4.2 app models user, notecard, tag , tagging (for m-2-m relationship). tag can have multiple notecards , notecard can have multiple tags. card belongs user , tag doesn't belong user. how can scope tags user has used? want have index of tags , index of tags user has used.
thanks!
here schema, don't have idea on how implement clause index tags user has used. give idea, i'm looking this
def index_of_used_tags #take tags, return have cards 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) enable_extension "plpgsql" create_table "cards", force: :cascade |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 |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 |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 |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 |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade |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
you can set has_many through
relationship between user , tag
class user < activerecord::base has_many :comments has_many :folders has_many :cards has_many :tags, through: :cards end
then user.tags
give tags user has used.
Comments
Post a Comment