user can provide more than one service
That means User
is associated to Service
model with has_many
relationship, i.e., your models should look like:
class User < ActiveRecord::Base
has_many :services
## ...
end
class Service < ActiveRecord::Base
belongs_to :user
## ...
end
Next, you must have a foreign_key user_id
in services
table.
Now i want to search for the Users who are residing in a particular location and providing a particular service.
You can achieve this result with the below mentioned query. Make sure to set the local variables location_name
and service_name
before executing this query:
User.joins(:services).where('users.location_name = ? and services.service_name = ?', location_name ,service_name)