Rails 5:用户与他作为版主的页面之间的关联



在我的rails应用程序中,我有一个用户模型和一个硬币模型,硬币模型是用户可以审核的页面。用户应该能够审核多个页面。

因此,如果有Coin_A、Coin_B、Coin_C和Coin_D, 用户 1 可以审核Coin_A和Coin_D,用户 2 可以审核Coin_B

我已经在两个模型之间建立了has_and_belongs_to_many关联。

现在我试图弄清楚如何为页面分配版主。理想情况下,我希望能够从用户个人资料页面上的硬币列表中进行选择,以将用户添加为版主。has_and_belongs_to_many是去这里的正确方式吗?我该怎么做呢?

用户.rb

class User < ApplicationRecord
acts_as_votable
has_many :questions, dependent: :destroy
has_many :events, dependent: :destroy
has_many :links, dependent: :destroy
has_many :posts, dependent: :destroy
has_and_belongs_to_many :coins
has_and_belongs_to_many :users
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, authentication_keys: [:login]
validates :username, presence: :true, uniqueness: { case_sensitive: false }
validates_format_of :username, with: /^[a-zA-Z0-9_.]*$/, :multiline => true
validate :validate_username
def validate_username
if User.where(email: username).exists?
errors.add(:username, :invalid)
end
end
def login=(login)
@login = login
end
def login
@login || self.username || self.email
end
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
elsif conditions.has_key?(:username) || conditions.has_key?(:email)
where(conditions.to_h).first
end
end
end

硬币.rb

class Coin < ApplicationRecord
validates :currency_name, presence: true
has_many :questions, dependent: :destroy
has_many :events, dependent: :destroy
has_many :links, dependent: :destroy
mount_uploader :picture, PictureUploader
has_and_belongs_to_many :genres
validate :picture_size
private
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "Picture must be smalled than 5MB.")
end
end
end

coins_controller.rb

class CoinsController < ApplicationController
load_and_authorize_resource param_method: :question_params
before_action :find_coin, only: [:edit, :update, :destroy ]
before_action :authenticate_user!, except: [:index, :create, :show]
def index      
@search = Coin.ransack(params[:q])
@coins = @search.result(distinct: true)
end
def new
@coin = Coin.new
end
def create
@coin = Coin.new(coin_params)
if @coin.save
flash[:success] = "Coin created"
redirect_to @coin
else
render 'new'
end
end
def show
@coin = Coin.find(params[:id])
end
def edit
authorize! :update, @coin
end
def update
if @coin.update(coin_params)
redirect_to @coin
else
render 'edit'
end     
end
def destroy
Coin.find(params[:id]).destroy
redirect_to coins_url
end
private
def coin_params
params.require(:coin).permit(:currency_name, :currency_abbrev, :working_product, :founder, :mineable, :date_of_ico, :website, :accepted, :picture, :question1, :question2, :question3, :question4, genre_ids:[])
end
def find_coin
@coin = Coin.find(params[:id])
end
end

我不确定我是否理解你的问题。 硬币和版主是什么关系? 听起来硬币有一个版主。 如果是这种情况,那么您可能需要

class Coin...
belongs_to :moderator, class_name: 'User'
class User...
has_many :moderated_coins, class_name: 'Coin'

最新更新