Active Admin Filters no viewing - ruby-on-rails

So I'm in Active Admin and in the following model a Degree belongs to a user and a User has many degrees
For a User we have a site_id that identifies where the User works in a Site table
So in my filter in Active Admin's degree model, I can't seem to pull a list of sites that someone can filter from in the Active Admin UI.
Here are my filters so far:
major
institution
completion_date
These are straight from the Degree tables
:user_active comes from Users table with a boolean attribute for an an Active
ActiveAdmin.register Degree do
belongs_to :user, :optional => true
menu :parent => 'Users'
config.sort_order = 'users.last_name_asc'
filter :user_active, :as => :select
**filter :site_id, collection: -> { User.all }, label: 'sites'**
filter :degree_type
filter :major
filter :institution
filter :completion_date
I've tried this as well
filter :site_id, :as => :select, collection: -> { User.all }, label: 'sites'
and no error message
I've also tried something like this with no error message, but nothing in the UI comes through again
filter :site, label: "Site", :as => :select, :collection => User.site_id
I've tried this and get the following errors:
filter :site_id, label: "Site", :as => :select, :collection => User.all
undefined method `site_id_eq' for Ransack::Search<class: Degree, base: Grouping <combinator: and>>:Ransack::Search
Any help here?
Update
I did some work-around and this is what I end-ed up with
filter :user_site_id, label: "site", :as => :select, :collection => User.all.map{|u| u.site}.map{|s| s.city if s.present? }.uniq.compact
The collection says
Iterated through all the users and call u.site (because a site_id
belongs to a User so there's a Site table that'll take each instance
of a user)
Since we're now at each Site object, map each of those
values and retrieve the city name for each Site object if it's
present...
I want to only filter the uniq values and .compact is
getting rid of the nil values in the array
I think there's a better solution because I'm going through so many iterations? Anyone have any other ideas? I'm going to make this an instance method on the Users model after the refactor.

Try this out:
filter :site_id, as: :select, collection: -> { User.pluck(:site_id) }, label: 'Sites'

Related

Filter the Dropdown Input in ActiveAdmin Rails

So I have a Company, Subcompany models, and I use the Brand model as the masterlist for all company and subcompanies. Right now, when the admin user creates a new company, they have to use the dropdown list of brands to put a new company or subcompany into the list.
This becomes unscalable as there are 10000's of companies. The easiest way to filter out companies is to filter by category. so assuming all brands have a category attribute, I was wondering if there is any way, within the ActiveAdmin framework, to allow the admin user to filter the dropdown list by a category?
Here is what I have so far, it allows me to do a drop down on all the brands. But I want a way for the user to dynamically shrink the list by picking a category.
form do |f|
f.inputs do
f.input :name, :as => :select, :collection => Brand.all.collect {|brand| brand.name }
f.has_many :sub_companies, allow_destroy: true do |sub|
sub.input :name, :as => :select, :collection => Brand.all.collect {|brand| brand.name}
end
end
actions
end
There is no build in way, you can do one of the following thinks:
User select2, a suggest/search supporting select field, you can find some integration help here
You can write a javascript to fill a second select by the value of the first one.

Create a drop down menu using a Table Attribute in Rails Active Admin

I need to create a dropdown field(membership_code), whose values are contained on a different table called members.
Schema
prereg
id
membership_code(string) not a foreign key
verification_code
members
id
membership_code
Prereg Active Admin Model
ActiveAdmin.register Prereg do
form do |f|
f.inputs "Preregistered Users" do
f.input :verification_code
f.input :email
#THIS LINE NEEDS TO BE CHANGED TO LIST DOWN THE MEMBERSHIP_CODE FROM MEMBERS
# f.input :membership_code, :as => :select, :collection => Members.all()
end
f.actions
end
To add, I was planning to have this logic wherein whenever you create a Prereg record, the selected "membership_code" would be deleted from the members.membership_code list.
How is this done in ActiveAdmin? Sorry I haven't found any good resource for DB Hooks and I'm still new to Rails.
I think you are looking for something as follows:
f.input :membership_code, as: :select, collection: Member.all.map(&:membership_code)
try this
f.input :membership_code, :as => :select, :collection => Members.select(:membership_code)
Thanks

Set multiple attributes on basis of results of a :select in rails form

I have a select box on my form which retreives an object with the following attributes:
:id, :v2_read_code, :v2_term
The select code is:
f.inputs "Tests" do
f.has_many :eqa_material_tests, :allow_destroy => true, :heading => 'Tests In EQA Material' do |cf|
cf.input :test_id, :as => :select, :collection => Hash[Test.all.order(default: :asc).map{|b| [b.v2_term,b.id]}]
end
end
Where I am storing the test id in a model/table with the following structure:
eqa_material_tests
id, test_id, eqa_material_id
In addition to storing the test_id, I'd also like to store the v2_read_code and v2_term as I'd like to keep a copy of these items if possible.
Is this possible?
After a nights sleep I've realised I'm approaching this wrong. I can do this using an active record callback like after_create

Rails 3 Model.all alternative?

I have a number of Model.all queries in collections in my view (using them for drop down lists to populate a form). Some of the lists returned have many records and it's taking a long time to load the edit view as a result.
Is there an alternative to calling .all in the following?
= f.input :descriptivedetail_primarycontenttype, :label => "Content type", :as => :select, :collection => Contenttype.all, :value_method => :code
I can't use a scope as I get the error
undefined method 'first' for :allisbns:Symbol
where :allisbns is
scope :allisbns, Isbn.all
in the model.
Thanks!

How can I retrieve information from an active record with collection select?

I have 3 models (Users - Membership - Community)
The users can become members to many communities. For this I made a Membership that contain the user_id, community_id.
After connected, the user have to choose a community. The model User as a community_id that contain that unique community.
When editing, he would be able to change this community.
If I do this :
<%= f.collection_select :community_id, Community.find(:all), :id, :name, { :allow_blank => 'Select a community' }, :style => "width: 200px;" %>
All the communities happier, also that who he is not member.
I tried this :
<%= f.collection_select :community_id, Membership.find(:all), :community_id, :id, { :allow_blank => 'Select a community' }, :style => "width: 200px;" %>
But I show only the number (:id) of the Membership…
How can I join this id with the name of the community ?
Not sure if this will work but try it out:
member.rb # add a method to the member model that returns the
def community_name
community.name
end
#view
<%= f.collection_select :community_id, Membership.find(:all, :include => :community), :community_id, :community_name, { :allow_blank => 'Select a community' } %>
The :include option prefetches all communities in the membership collection in one query.
I think you were closer with your first attempt but instead of finding all communities you need to just find communities that the user is a member of. So instead of Community.find(:all) you would use:
Community.find(:all,
:includes => :memberships,
:conditions => ['memberships.user_id = ?', #user.id])
This assumes that you have an #user variable set up for your view. You need this to restrict the find to just communities that your user is a member of.
It also assumes that there's an association on Community: has_many :memberships. I've guessed you've got that already from the question.

Resources