Rails 3 Model.all alternative? - ruby-on-rails

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!

Related

Active Admin Filters no viewing

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'

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

How do I search for multiple records in a search form?

I am trying to allow the user to be able to choose multiple records in a field on the search form.
Something like this:
<%= f.input_field :neighborhood_id, collection: Neighborhood.order(:name), :url => autocomplete_neighborhood_name_searches_path, :as => :autocomplete, 'data-delimiter' => ',', :multiple => true, :class => "span8" %>
It sends it to my search model like this: #search = Search.create!(params[:search])
This is what the Search.rb model does with it:
key = "%#{keywords}%"
listings = Listing.order(:headline)
listings = listings.includes(:neighborhood).where("listings.headline like ? or neighborhoods.name like ?", key, key) if keywords.present?
listings = listings.where(neighborhood_id: neighborhood_id) if neighborhood_id.present?
listings
The issue is that this is just accepting 1 neighborhood_id, so I am getting this error when I choose multiple objects:
undefined method `to_i' for ["Alley Park, Madison"]:Array
Where Alley Park and Madison are the names of 2 neighborhoods, not the IDs.
So how do I get this working?
Thanks.
Edit 1
The issue seems to not be in the lookup of the params[:search] per se, but rather in the conversion of the form input to an array of entries. I tried changing the search method to be something like:
listings = listings.includes(:neighborhood).where("neighborhoods.name like ?", neighborhood_id) if neighborhood_id.present?
Don't get hung up on the fact that I am looking up neighborhood.name and passing in neighborhood_id. I just did that because I know that the params for the field neighborhood_id were actually the names of the neighborhood. If this had worked, I would have refactored some stuff, but it didn't. So don't get hung up on that.
But that still returns the error undefined method 'to_i'....
Also, I still get that error even if I just pass in 1 option.
listings = listings.where("neighborhood_id in (?) ", neighborhood_id)
You can get the id instead of neighborhood names from the input field like this:
<%= f.input_field :neighborhood_id, collection: Neighborhood.order(:name), :url => autocomplete_neighborhood_name_searches_path, :as => :autocomplete, 'data-delimiter' => ',', :multiple => true, :class => "span8", :input_html => { :id => "neighborhood_id" } %>

Rails Simple-Form group_method

I came across the following on the simple_form github repo:
f.input :country_id, :collection => #continents, :as => :grouped_select, :group_method => :countries
The thing that caught my attention was the :group_method wich would be exceptionally usefull when creating a selectbox that gives options based on what's in the database. The only thing I can't work out is what kind of input the :group_method expects, and where to put the method.
For instance, I want to create a selectbox for the table column :product_type. I imagine I would write something like this in my simple form:
= f.input :product_type_contains, :collection => #products, :as => :grouped_select, :group_method => :product_types
where :product_type would be the method that is being called. But I don't know what kind of method I should write, what kind of result simple_form expects, and if I should put it in the Product class, Product.rb. Any help would be greatly appreciated!
According to the test suite, simple_form seems to expect the type of arrays or hashes that you would use with grouped_options_for_select:
test 'grouped collection accepts group_label_method option' do
with_input_for #user, :tag_ids, :grouped_select,
:collection => { ['Jose', 'Carlos'] => 'Authors' },
:group_method => :first,
:group_label_method => :last
[...]
test 'grouped collection accepts label and value methods options' do
with_input_for #user, :tag_ids, :grouped_select,
:collection => { 'Authors' => ['Jose', 'Carlos'] },
:group_method => :last,
:label_method => :upcase,
:value_method => :downcase
[...]
Presumably, you could write a class method on Product.rb that creates a structure similar, or even try using grouped_options_for_select(#products)...
Hope this gets you on the right path.

Resources