I'm working on an application and I try to implement a filter with filterrific on the post index.
The filter sort the post with user params : mother_tongue, locality, and availability.
I CreateJoinTableUsersPosts
Post belongs_to :user
User has_one :post
Post.rb :
filterrific(
default_filter_params: {},
available_filters: [
:with_mother_tongue,
:with_locality,
:with_start_time_gte
]
)
scope :with_mother_tongue, -> (search_string) { joins(:users).where("users.mother_tongue LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
scope :with_locality, -> (search_string) { joins(:users).where("users.locality LIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
scope :with_start_time_gte, -> (ref_date) { joins(:availabilities).where('availabilities.start_time >= ?', ref_date) }
The filter works on users index but not on posts index with the error :
Can't join 'Post' to association named 'users'; perhaps you misspelled
it?
Do you know why there is this error ?
I think you don't need the join table between users and posts, drop it. And your join should start working.
Other than that, if you are using devise gem, you already have #user available in all controllers inherting from ApplicationController. If not, then it's a whole different story. Research that, there are plenty of resources on that topic.
Don't hesitate to comment, though.
Related
I am trying to add a search query in filterrific gem. I have user model with a method that returns all diagnosis of the user.
def diagnosis
self.consultations.map(&:diagnosis).uniq.compact
end
I want to filter based on this method. Filtering by name is working fine because it's attribute of user table.
Works
scope :with_name, -> (search_string) {
where("users.name ILIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
Now are there any way to filter by diagnosis by adding it in query like? of course the below one doesn't work. Or by putting any SQL query?
Need to query based on the diagnosis method
scope :with_name, -> (search_string) {
where("users.diagnosis ILIKE ?", (search_string.to_s.gsub('*', '%') + '%').gsub(/%+/, '%'))
}
I have a model Deployment, it has following relations:
belongs_to :user
belongs_to :user_group, optional: true
I want to be able to define scope for easier filtering. I can easily filter by user, something like this:
scope :by_owner, -> (ow) { joins(:user).where("users.name like ?", "%#{ow}%") }
But what I really want to use user_group first if association exists and if it doesn't, then fall back on user. Logic would work like this:
If this deployment belongs to at least one user_group
scope :by_owner, -> (ow) { joins(:user_group).where("user_groups.name like ?", "%#{ow}%") }
else
scope :by_owner, -> (ow) { joins(:user).where("users.name like ?", "%#{ow}%") }
end
I am guessing that I have to achieve it somehow with an SQL query, but I can't seem to figure it out. Any suggestions?
Thanks!
Update
I got a bit closer with the following query:
scope :by_owner, -> (ow) { joins(:user, :user_group).where("(users.display_name like ?) OR (user_groups.name like ?)", "%#{ow}%", "%#{ow}%") }
But it's still not it because 1st of all the records that don't have user_groups association are ignored in the response and also it searches by user even if user_group is defined. Still looking for suggestions if there are any.
I'm struggling to either return the union of two scopes or combine them into one in my Rails app. The outcome I'm looking for is to retrieve all orders that satisfy either of the scopes: Order.with_buyer_like or the Order.with_customer_like. That is I'm looking for the union of the sets. Importantly, I need these to be returned as an ActiveRecord::Relation, rather than an array. Otherwise, I'd just do
results = Order.with_buyer_like + Order.with_customer_like
I've also tried to use the "or" operator but get an error:
Order.with_seller_like('pete').or(Order.with_customer_like('pete'))
ArgumentError: Relation passed to #or must be structurally compatible. Incompatible values: [:joins]
I've also tried combining the scopes into one, but I can't quite get it to work.
Here's my setup:
class Company
has_many :stores
end
class Store
belongs_to :company
end
class Order
belongs_to :buying_store, class_name: 'Store', foreign_key: 'buying_store_id', required: true
belongs_to :selling_store, class_name: 'Store', foreign_key: 'selling_store_id', required: true
scope :with_buyer_like, ->(search_term) { joins(buying_store: [:company], :customer).where(['stores.name LIKE ? OR companies.name LIKE ?', "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%"]) }
scope :with_customer_like, ->(search_term) { joins(:customer).where(['first_name LIKE ? OR last_name LIKE ? OR mobile_number LIKE ? OR email LIKE ?', "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%", "%#{search_term.gsub(/ /, '_').downcase}%"] ) }
end
This blog has good explanation of issue you are facing.
To summarize here, you have to make sure the joins, includes, select (in short structure of AR data to be fetched) is consistent between both the scopes.
This syntax is the way to go as long as you make sure both the scopes have same joins.
Order.with_seller_like('pete').or(Order.with_customer_like('pete'))
To take first step, check if this works(not recommended though):
Order.where(id: Order.with_seller_like('pete')).or(Order.where(id: Order.with_customer_like('pete')))
I would recommend to move away from joins and use sub-query style of querying if you are looking for only Order data in output.
So I'm trying to improve the search feature for my app
My model relationships/associations are like so (many>one, one=one):
Clients < Projects < Activities = Assignments = Users
Assignments < Tasks
Tasks table has only a foreign key to assignments.
Search params look something like this:
params[:search]==User: 'user_handle', Client: 'client_name', Project: 'project_name', Activity: 'activity_name'
So I need to porbably search Clients.where().tasks, Projects.where().tasks and so on.
Then I need to somehow concatenate those queries and get rid of all the duplicate results. How to do that in practice however, I have no clue.
I've been hitting my head against a brick wall with this and internet searches didn't really help... so any help is greatly apreciated. Its probably a simple solution too...
I am on rails 4.2.5 sqlite for dev pg for production
A few things I would change/recommend based on the code in your own answer:
Move the search queries into scopes on each model class
Prefer AREL over raw SQL when composing queries (here's a quick
guide)
Enhance rails to use some sort of or when querying Models
The changes I suggest will enable you to do something like this:
search = search_params
tasks = Tasks.all
tasks = tasks.or.user_handle_matches(handle) if (handle = search[:user].presence)
tasks = tasks.or.client_name_matches(name) if (name = search[:client].presence)
tasks = tasks.or.project_name_matches(name) if (name = search[:project].presence)
tasks = tasks.or.activity_name_matches(name) if (name = search[:activity].presence)
#tasks = tasks.uniq
First, convert each of your queries to a scope on your models. This enables you to reuse your scopes later:
class User
scope :handle_matches, ->(handle) {
where(arel_table[:handle].matches("%#{handle}%"))
}
end
class Client
scope :name_matches, ->(name) {
where(arel_table[:name].matches("%#{name}%"))
}
end
class Project
scope :name_matches, ->(name) {
where(arel_table[:name].matches("%#{name}%"))
}
end
class Activity
scope :name_matches, ->(name) {
where(arel_table[:name].matches("%#{name}%"))
}
end
You can then use these scopes on your Task model to allow for better searching capabilities. For each of the scopes on Task we are doing an join (inner join) on a relationship and using the scope to limit the results of the join:
class Task
belongs_to :assignment
has_one :user, :through => :assignment
has_one :activity, :through => :assignment
has_one :project, :through => :activity
scope :user_handle_matches, ->(handle) {
joins(:user).merge( User.handle_matches(handle) )
}
scope :client_name_matches, ->(name) {
joins(:client).merge( Client.name_matches(name) )
}
scope :activity_name_matches, ->(name) {
joins(:activity).merge( Activity.name_matches(name) )
}
scope :project_name_matches, ->(name) {
joins(:project).merge( Project.name_matches(name) )
}
end
The final problem to solve is oring the results. Rails 4 and below don't really allow this out of the box but there are gems and code out there to allow this functionality.
I often include the code in this GitHub gist in an initializer to allow oring of scopes. The code allows you to do things like Person.where(name: 'John').or.where(name: 'Jane').
Many other options are discussed in this SO question.
If you don't want include random code and gems, another option is to pass an array of ids into the where clause. This generates a query similar to SELECT * FROM tasks WHERE id IN (1, 4, 5, ...):
tasks = []
tasks << Tasks.user_handle_matches(handle) if (handle = search[:user].presence)
tasks << tasks.or.client_name_matches(name) if (name = search[:client].presence)
tasks << tasks.or.project_name_matches(name) if (name = search[:project].presence)
tasks << tasks.or.activity_name_matches(name) if (name = search[:activity].presence)
# get the matching id's for each query defined above
# this is the catch, each call to `pluck` is another hit of the db
task_ids = tasks.collect {|query| query.pluck(:id) }
tasks_ids.uniq!
#tasks = Tasks.where(id: tasks_ids)
So I solved it, it is supper sloppy however.
first I wrote a method
def add_res(ar_obj)
ar_obj.each do |o|
res += o.tasks
end
return res
end
then I wrote my search logic like so
if !search_params[:user].empty?
query = add_res(User.where('handle LIKE ?', "%#{search_params[:user]}%"))
#tasks.nil? ? #tasks=query : #tasks=#tasks&query
end
if !search_params[:client].empty?
query = add_res(Client.where('name LIKE ?', "%#{search_params[:client]}%"))
#tasks.nil? ? #tasks=query : #tasks=#tasks&query
end
if !search_params[:project].empty?
query = add_res(Project.where('name LIKE ?', "%#{search_params[:project]}%"))
#tasks.nil? ? #tasks=query : #tasks=#tasks&query
end
if !search_params[:activity].empty?
query = add_res(Activity.where('name LIKE ?', "%#{search_params[:activity]}%"))
#tasks.nil? ? #tasks=query : #tasks=#tasks&query
end
if #tasks.nil?
#tasks=Task.all
end
#tasks=#tasks.uniq
If someone can provide a better answer I would be forever greatful
I have User and Album models with HABTM relationship
class Album < ActiveRecord::Base
has_and_belongs_to_many :users
class User < ActiveRecord::Base
has_and_belongs_to_many(:albums)
I'd like to find all the albums that are stored in the database but not associated with a particular user.
So far my code is like this:
Album.all(:order => "albums.created_at DESC", :include => "users", :limit => limit, :conditions => ["users.id != ? AND users.id IS NOT NULL", current_user.id])
but for some reason this is not working. It's returning albums that are associated with current_user.
here take a look at this ouptput from the console.
Check the users id i first fetch.
Then i fetch albums which should not have the users id
I then find one of the listed albums and ask it to return the associated users
one of those associated users is the one from above and shouldnt be there.
Can anyone help with the above?
I usually try to stay away sub-selects but I can't seem to get it to work any other way:
class Album < ActiveRecord::Base
scope :without_user, lambda{|u| where("#{quoted_table_name}.id NOT IN (SELECT `albums_users`.album_id FROM `albums_users` WHERE `albums_users`.user_id = ?)", u.id) }
end
user = User.find(30)
Album.without_user(user)
Assuming you created table albums_users to hold relationship data:
Album.includes(:users).
where(["albums_users.user_id IS NULL OR albums_users.user_id != ?", user_id])
I think it will generate SQL along the lines of
SELECT *
FROM albums LEFT OUTER JOIN albums_users ON albums.id = albums_users.album_id
WHERE albums_users.album_id IS NULL OR albums_users.album_id != #{user_id}
Try:
:conditions => ["users.id <> ? AND users.id IS NOT NULL", current_user.id]
A non-sql solution would be:
Album.all.reject{|album| user.albums.include?(album)}
Obviously, if you have tens of thousands of rows in your database, you might not want to do this.
Might do something like this, too:
Album.where(["id NOT IN (?)", user.albums_ids])
But if your users have a lot (say hundreds) of albums, you shouldn't do this either.
Just throwing in easy solutions if you're out for one of those.