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?
Related
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
I have a structure like this:
A Position has_many Requests
A Request belongs_to a User
A Group has_many :members, through: :memberships, source: :user
I would like to get all groups where not all members has a particular request.
UPDATE: I have a form were I can search for individual users and groups.
I can click a user and send him/her a request (and by that, create a request).
I can also click a group and send every user in that group a request in the same manner.
The position has multiple requests, and one request has one user.
As you can see, I can make sure that no user, having a request to a given position is shown in the search result.
Bottom line: If every member in a group has a request to a given position, the group should not appear in the search result. If not all members of that group has a request, the group should be shown.
Right now I manage to get individual users that doesn't belong to the request:
#users = User.search(query_str).where.not(id: #position.requests)
I'm trying to do something like
#groups = Group.search(query_str).where.not(
#position.request.include?(all users of a Group))
I made a method in Position < ActiveRecord::Base:
def user_exists?(user)
# Check if user exists in position
self.requests.map(&:user_id).include?(user.id)
end
def all_users_exists?(users)
users.each do |user|
unless self.user_exists? user
return false
end
end
true
end
The way I can come up with to filter out groups using this method is in the views when iterating over all groups, which feels like a terrible solution.
<% #groups.each do |group| %>
<% unless #position.all_users_exists? group.members %>
# Print list-item...
Could anyone please help me filtering out the groups with all members in a request when querying instead?
Oh, and I'm using rails 4.2.5
Thanks!
I am trying out Tire gem for my Demo Rails app to implement ElasticSearch.
So, here are my models associations :-
A User belongs to many UserGroups. And Every UserGroup has many Post associated with it.
So, this is what I do to show all the posts for a User in posts_controller.rb
def index
#user_groups = current_user.user_groups
for group in #user_groups
for p in group.posts
#posts = #posts.to_a.push p
end
end
end
Now, I want to add search functionality to it. A User may be able to search for a Post from all the Post that are visible to him.
So, I have two questions which are connected to each other.
Q1. How, do I add the search functionality by using the Tire gem for the User so that the user may search from the Posts that are visible to him ?
Tire allows to directly search on a model using
#posts = Post.search(params[:query])
But, I want to search from an array.
Q2. And Secondly, Is my approach correct, by first storing the concerned Posts in an array and then use Tire to search from that array ?
The Rails application I'm working on, has two central pieces: users and groups. A user registers through a form, and is added to the users table.
The groups table has four primary columns: name, title, description, and membership.
Groups are handled in two steps.
Step1: the admin creates the group (name, title, description).
Step2: Membership is a JSON field. Admin needs to log in using a second form, and add/edit that JSON structure's fields: group owner, group admins, members, parse / re-parse into a JSON and update the groups table.
The user signup, and groups creation form are working fine. The problem is the second form: creating the group's membership. I am not really sure how to proceed.
What I did is create the link and routes to properly select from the groups, and produce a REST call with the correct params by adding additional routes and links on the Show page. I verified that the l ink is correct, when I hover over it, I see http://myapp.com/groups/3/admin (3 is the correct ID in the groups table).
The methods in my groups_controller.rb are similar to the following:
def show
#group = Group.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #group }
end
end
I created a groups_admin.html.erb and put it in the views/groups folder. It has in it the logic to construct a set of drop down boxes, allowing the admin to pick from the users list to fill the group membership (the functions are in the groups helper).
What I don't know is how to complete it with a form declaration, etc. in groups_admin.html.erb and what to add to the groups_controller.rb to cause an UPDATE to that particular row (after I turn the returned fields into a JSON). So far, here's what I have added to it
def admins
end
Changing the groups table schema and creating additional groups_admins, groups_owners, groups_members tables is not an option. Any ideas?
You're looking for associations. In this case you probably want a group has_many users and a user has_many groups. Or if you want a user to be able to be in multiple groups you're looking for a has_and_belongs_to_many association. You can read up on this here: http://guides.rubyonrails.org/association_basics.html
I'm using rails 3.2.3 and have a questions about queries.
I've read that it is favorable using arel instead of named scopes.
Basically in my app, when a user logs in, I want him to see the products that he created.
So instead of having in my controllers index:
products=Product.find(:all)
I was looking for something like
products=Product.find(:all, :conditions....)
The thing is, my User and Product models have a HABTM relation (it really has to be) and I don't know how to join those tables so that only the products inserted by the current_user are displayed (the insertion is working correctly)
Do I have to add a search method in my Product model?
Or this can be accomplished by passing :conditions in the index controller?
Basically the logic is:
->Get all the products
->inner joining with the products_users HABTM table and get all the products where products_users.user_id = current_user.id. Return the results.
I don't know if I'm missing something here...any tips on how to code this? I'm kind of confused.
if user_sighed_in?
#products = current_user.products
else
#products = Product.scoped
end
ofc u have to define association in User model
has_many :products
If you have associated User and Products models - this code #products = current_user.products will return products of current_user.
To find all the products of current user this will do the trick
current_user.products