首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails 5:用户与他作为版主的页面之间的关联

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

Stack Overflow用户
提问于 2018-06-02 06:26:42
回答 1查看 27关注 0票数 0

在我的rails应用程序中,我有一个用户模型和一个Coin模型,Coin模型是用户可以调整的页面。用户应该能够调整多个页面。

因此,如果有Coin_A、Coin_B、Coin_C和Coin_D,User1可以调节Coin_A,Coin_D和User2可以调节Coin_B

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

现在我被卡住了,试图弄清楚如何给页面分配版主。理想情况下,我希望能够从用户配置文件页面上的硬币列表中选择,以添加作为版主的用户。has_and_belongs_to_many是去这里的正确方式吗?我该怎么做呢?

User.rb

代码语言:javascript
复制
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

Coin.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-06-02 07:13:01

我不太明白你的问题。硬币和版主之间的关系是什么?听起来硬币有一个主持人。如果是这样,那么您可能需要

代码语言:javascript
复制
class Coin...
  belongs_to :moderator, class_name: 'User'

class User...
  has_many :moderated_coins, class_name: 'Coin'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50651700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档