Search code examples
ruby-on-railsrubyrails-admin

Rails admin dont show any record in Ad model


Rails admin not show any record. Even there is 102 record persist.

enter image description here

Even not listed in dashboard

enter image description here

But i can see one ad

enter image description here

My Ad model

class Ad < ApplicationRecord
  extend FriendlyId

  belongs_to :category
  belongs_to :user
  has_many :ad_images
  alias_attribute :images, :ad_images
  enum status: [:draft, :review, :published, :moderate]

  friendly_id :title, use: :slugged
  acts_as_paranoid
  after_initialize :set_default_status, :if => :new_record?

  scope :latest, -> { order(time_start: :asc) }
  scope :status_draft, -> { where(status: Ad.statuses[:draft]) }
  scope :status_published, -> { where(status: Ad.statuses[:published]) }
  scope :current_displayed, -> { where('time_start < ? AND time_end > ?', Time.now, Time.now) }

  scope :show_active, -> { status_published.latest.current_displayed }
  scope :show_draft, -> { status_draft.latest.current_displayed }

  def should_generate_new_friendly_id?
    slug.blank? || title_changed?
  end

  def set_default_status
    self.status ||= :draft
  end

  def related
    category.ads.where.not(id: id).show_active.limit(6)
  end
end

my rails_admin.rb

RailsAdmin.config do |config|

  ### Popular gems integration

  ## == Devise ==
  config.authenticate_with do
    warden.authenticate! scope: :user
  end

  ## == method to call for current_user ==
  config.current_user_method(&:current_user)

  ## == CancanCan ==
  config.authorize_with :cancancan

  ## == Pundit ==
  # config.authorize_with :pundit

  ## == PaperTrail ==
  # config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0

  ### More at https://github.com/sferik/rails_admin/wiki/Base-configuration

  ## == Gravatar integration ==
  ## To disable Gravatar integration in Navigation Bar set to false
  # config.show_gravatar = true

  config.parent_controller = 'ApplicationController'

  config.actions do
    dashboard                     # mandatory
    index                         # mandatory
    new
    export
    bulk_delete
    show
    edit
    delete
    show_in_app

    ## With an audit adapter, you can add:
    # history_index
    # history_show
  end

  # config.model 'Ad' do
    # list do
      # field :title
      # field :created_at
    # end
  # end
end

cancancan ability file

# frozen_string_literal: true

class Ability
  include CanCan::Ability

  def initialize(user)
    # Define abilities for the passed in user here. For example:
    #
    user ||= User.new # guest user (not logged in)
    if user.admin?
      can :access, :rails_admin
      can :manage, :all
    end
    #
    # The first argument to `can` is the action you are giving the user
    # permission to do.
    # If you pass :manage it will apply to every action. Other common actions
    # here are :read, :create, :update and :destroy.
    #
    # The second argument is the resource the user can perform the action on.
    # If you pass :all it will apply to every resource. Otherwise pass a Ruby
    # class of the resource.
    #
    # The third argument is an optional hash of conditions to further filter the
    # objects.
    # For example, here the user can only update published articles.
    #
    #   can :update, Article, :published => true
    #
    # See the wiki for details:
    # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
  end
end

I dont do any fancy config setup for rails_admin and ad.rb model. This happens in development environment and production. I think this is because rails_admin hide something i dont know. I dont know what to do.


Solution

  • I solved this by renaming "Ad" model to "Item"