I have the following ActiveAdmin model
class AdminUser < ActiveRecord::Base
has_many :challenges
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable
end
and the following Challenge model
class Challenge < ActiveRecord::Base
belongs_to :subdomain
belongs_to :admin_user
has_many :comments, :dependent => :destroy
end
I defined the following method in my application.html.erb
helper_method :all_admin_users
def all_admin_users
@users = AdminUser.all
end
Adminuser has the following attributes :id,:name,:email Now when i try to access the challenges of a particular User
<ul>
<% all_admin_users.each do |user| %>
<li> <%= link_to user.name ,user.challenges%></li>
<br />
<% end %>
</ul>
I get the following error "undefined method `to_model' for Challenge::ActiveRecord_Associations_CollectionProxy
here's my routes.rb file
Rails.application.routes.draw do
resources :comments
devise_for :admin_users, ActiveAdmin::Devise.config
get 'subdomains/index'
get 'subdomains/edit'
get 'subdomains/new'
get 'subdomains/show'
get 'home/index'
root 'home#index'
resources :challenges
resources :subdomains
ActiveAdmin.routes(self)
end
First in your routes file you need to add action to list users-challenged
In routes.rb at end add
get 'users/:user_id/challenges' => 'challenges#user_challenges', as: :user_challenges
# this way you will have action to view users challenges
# inside your challenges controller
In your challenges_controller.rb add action user_challenges
def user_challenges
@challenges = Challenge.where(admin_user_id: params[:user_id])
end
Create view for this action, and display your challenges
Then you can create link for this action
<ul>
<% all_admin_users.each do |user| %>
<li> <%= link_to user.name , user_challenges_path(user.id)%> </li>
<br />
<% end %>
</ul>
Instead of user.challenges use user.challenge
UPDATE
<ul>
<% all_admin_users.each do |user| %>
<% user.challenges.each do |challenge| %>
<li> <%= link_to user.name ,challenges_path(user_id: user.try(:id)) %></li>
<br />
<% end %>
<% end %>
</ul>
challenges_controller.rb
def index
@challenges = Challenge.where(admin_user_id: params[:user_id])
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With