Active Admin: union of multiple filters - ruby-on-rails

I'm trying to create a new filter on AA to accomplish the following: a city has many properties; a property has many rooms. I'd like to create a filter on the rooms page where a user is able to filter the results by rooms in one or more cities. For example, if we have the cities of Chicago, Miami, and Montreal, I'd like the user to be able to filter for rooms in one city, both cities, or all three cities (so the union of the rooms in whichever cities they select). My question is whether or not this is possible with Active Admin and, if so, how to approach it.
Thanks!

This is possible, for this you should pass an option to filter that will allow multiple selection.
class Room < ActiveRecord::Base
belongs_to :city
end
ActiveAdmin.register Room do
filter :city, as: :select, multiple: true
end
Also answered on github issue thread https://github.com/activeadmin/activeadmin/issues/4743

Related

Ransack and display by category

I want to display users based on their type, so on the user overview page I have three headers: admins / moderators / members. Under each header I show the users that are part of that group. So I query on user_type and find all users that are belonging to this group.
Each member is also works at a certain company. I'm creating a filter that uses ransack to filter on that company:
= f.input :users_company_id_in, collection: Company.order(:name), label: 'Company'
In my controller I'm using ransack like this:
def index
#q = UserType.includes(:users, :active_users).ransack(params[:q])
#user_types = #q.result(distinct: true)
end
Since in my view I'm fetching the users by using #user_types.users, this query is not respecting the ransack parameters, so I'm seeing all users for every category that has someone working for that company.
What I'm trying to achieve is obviously to see only the users that work for a specific company and only display a certain user_type if there are people in the category after filtering.
Any ideas on how to achieve this?

getting .count of associated model

I have a Ticket.rb model, and School.rb model.
Ticket belongs_to School
School has_many Tickets
How can I use ActiveRecord to find out total .count of how many Tickets each School has?
I'm aware that Ticket.where(:school_id => -insert School ID here-).count will give me the individual count of each school but I'm using this to populate the data into a graph so I really need something more like School.tickets.count.
Is this possible without messing up my associations?
Thanks all..
Ticket.group(:school_id).count
This will give you the count of tickets for each school id with key as the school_id and the value as the ticket count
If you want to group by a different attribute on School, then
Ticket.joins(:school).group("schools.name").count
Eg output:
{3 => 10, 4 => 30}
If you have a specific School instance (such as #school), you can use
#school.tickets.count.
For better performance, you can use the group method in your controller outlined here.
So in your controller:
def someaction
#school_tickets = Ticket.group(:school_id)
end
and in your view you can loop through the #school_tickets such as:
#school_tickets.each do |school_ticket|
school_ticket.count
end

Populating a drop down list in ruby on rails and saving the result

I'm fairly new to RoR and having trouble wrapping my head around how to do this.
Basically I want to design a drop down menu that will dynamically populate a drop down of newspapers from the database. Once the paper is selected, I want the user to be able to select an issue category (ex: billing), then a specific issue_type (ex: credit card charge), then the contact type (email or phone) (total of 4 drop downs).
The issue category, issue_type, and contact_type all belong to Issuelog. Each Issuelog should belong to a specific newspaper, as per my model code. I want the user to be able to record the volume of each kind of contact for each type of issue for each paper, with a very standard set of selections available. The newspaper table will not change after the submission, it will simply create an Issuelog that will correlate to that particular paper (the id created by default - not sure if I need to create any additional keys in this scenario).
Issuelog
class Issuelog < ActiveRecord::Base
belongs_to :newspaper
attr_accessible :category, :contact_type, :issue_type
end
Newspaper
class Newspaper < ActiveRecord::Base
has_many :issuelogs
attr_accessible :affiliate_group, :name
end
I'm having trouble understanding how I will need to structure this overall to achieve what I want. Will I need to use JavaScript in my view, and does my model design make sense for what I'm trying to achieve?
In controller's action
#newspapers = Newspaper.find(:all)
In model there are many that you can use, You can use something like this.
<%= select("newspaper", "ids", #newspapers.collect {|p| [ p.name, p.id ] }, { :prompt => 'Select' }, :onChange => 'do_your_thing()') %>
I hope this helps, But tell if you need any clarification

Linking/Connecting two db rows in Rails

I'm trying to find a way of connecting two active record objects, not a full merge but somehow having them associated.
For example if I had two models, City and Restaurant, each city can have many restaurants. In this example if there are two City records, "Napoli" and "Naples" that represent the same city, I would like to connect them in the db so regardless of whether the user clicked on restaurants in "Napoli" or "Naples" they would be taken to the same page.
I apologize if I've explained this poorly, I can't fully articulate what I'm after without using an example.
I'm using Rails 3.2, ruby 1.9.2 and postgres
Thanks.
class City < ActiveRecord::Base
has_many :city_name, :dependent => :destroy
end
class CityName < ActiveRecord::Base
belongs_to :city
end
When you search for a city in your controller, you can check all the names and if any matches you render the same restaurant.
You can add a lookup field that two cities that are actually the same have in common (kind of ad-hoc solution, but, I think it works).
So, Napoli and Naples would have maybe a "City id" of 46, whereas "Mumbai" and "Bombay" would have a city id of 32.

Rails City Listings

I'm relatively new to rails and I'm working on a task which requires a similar functionality to the Cities tab here: http://haystack.com/cities . I've searched all the books and can't seem to understand the logic that makes all of this possible. The only thing I can think of is reverse engineer e-commerce code to do what I want. Does anyone have any bright ideas?
You're going to at least need to get a listing of city/state data, either from somewhere like geonames.org or zipcodeworld.com. You'll then have to map cities to states, states to countries, and then figure out a clean way to display that, much like the haystack.com site. I would guess top cities is either weighted by how many users have requested each city or weighted simply by supposed popular cities.
Mostly it will just involve how you relate each of those data types (City, State, Country) together. Actually displaying, apart from how to lay all that out, is simple then. Basically have a Cities, States and Countries table in your DB, then create models something like:
class Country
has_many :states
end
class State
belongs_to :country
has_many :cities
end
class City
belongs_to :state
end
EDIT:
To associate it with users: Assuming that a user can only belong to one city (although multiple would not be that difficult either), it would look something like:
class User
belongs_to :city
end
class City
belongs_to :state
has_many :users
end
You should be able to do things like:
usr = User.find(a_user_id)
usr.city #New York City
usr.city.state #New York
usr.city.state.country #United States
Perhaps something like this:
http://www.geonames.org/export/
is what you're looking for?

Resources