在我的項目我使用acts_as_follower
寶石,以實現用戶followable_type「ApplicationRecord」,而不是RoR中的應用「用戶」
所以在我的User類中的後續/未追蹤功能,我有
class User < ApplicationRecord
...
acts_as_follower
acts_as_followable
...
end
但在db表Follow
,該follower_type
是User
(這是正確的),而followable_type
是ApplicationRecord
我是,如果你非常感謝可以幫助我瞭解爲什麼,在此先感謝..
的一些信息:
follows_controller.rb:
class FollowsController < ApplicationController
def create
@user = User.find(params[:id])
if([email protected])
current_user.follow(@user)
redirect_to users_show_path(:id => @user.id)
flash[:notice] = "You are now following #{@user.username}."
end
end
def destroy
@user = User.find(params[:id])
if([email protected])
current_user.stop_following(@user)
redirect_to users_show_path(:id => @user.id)
flash[:notice] = "You are no longer following #{@user.username}."
end
end
end
schema.rb:
create_table "follows", force: :cascade do |t|
t.string "followable_type"
t.integer "followable_id", null: false
t.string "follower_type"
t.integer "follower_id", null: false
t.boolean "blocked", default: false, null: false
t.datetime "created_at"
t.datetime "updated_at"
t.index ["followable_id", "followable_type"], name: "fk_followables"
t.index ["follower_id", "follower_type"], name: "fk_follows"
end
follow.rb
class Follow < ActiveRecord::Base
extend ActsAsFollower::FollowerLib
extend ActsAsFollower::FollowScopes
# NOTE: Follows belong to the "followable" interface, and also to followers
belongs_to :followable, :polymorphic => true
belongs_to :follower, :polymorphic => true
def block!
self.update_attribute(:blocked, true)
end
end
你如何創建'association'?分享代碼的控制器和型號'follow_controller.rb' –
@ Md.FarhanMemon我已編輯的問題,共享代碼,謝謝 –
ApplicationRecord(如在標題)或ActiveRecord(如在說明中)? – MikDiet