Reputation: 373
In my rails app I have a User model and a Coin model, with the Coin model being pages that a user can moderate. The user should be able to moderate multiple pages.
So if there is Coin_A, Coin_B, Coin_C, and Coin_D, User1 can moderate Coin_A and Coin_D and User2 can moderate Coin_B
I've set up a has_and_belongs_to_many association between the two models.
Now I'm stuck trying to figure out how to assign a moderator to pages. Ideally I would like to be able to select from a list of Coins on the User profile page to add the user as a moderator. Is has_and_belongs_to_many the right way to go here? How could I go about doing this?
User.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
Coin.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
Upvotes: 0
Views: 31
Reputation: 2401
I'm not sure I understand your question. What is the relationship from coin to moderator? Sounds like coin has one moderator. If that's the case, then you may want
class Coin...
belongs_to :moderator, class_name: 'User'
class User...
has_many :moderated_coins, class_name: 'Coin'
Upvotes: 1