Rails Search Query Associated Model
Assuming that by following Rails convention you have tables named dogs
for Dog
model and owners
for Owner
model.
Update the search
method as below:
def self.search(search)
if search
joins(:owner).where('dogs.name LIKE ? or owners.name LIKE ?', "%#{search}%", "%#{search}%")
else
find(:all)
end
end
You need a join query between dogs
and owners
table in order to access owners.name
field
Rails 5. How to search in associated models?
Try this:
@countries = Country.joins(:cities).where("cities.name LIKE ?", "%#{params[:search]}%")
Query to get all data from associated model on rails
You can use select
to select the fields that you want:
User.joins(:student_profile).select('users.*, student_profiles.*')
*
is for selecting all fields
Or Choosing specific fields from student_profiles
table:
User
.joins(:student_profile)
.select('users.*, student_profiles.first_name, student_profiles.last_name')
But I would suggest reading about active model serializers since you dealing with JSON.
Rails querying an associated models scope
As i understand, you need to select categories with posts visible to an user. For that you need to do two things:
- Make mapping between user’s roles and post’s visible scopes
- Select categories for posts visible by user. In your case it’s easier to select that categories in two queries, without joins.
Try this code:
class Category < ApplicationRecord
has_many :posts
scope :with_posts_for_user, -> (user) do
where(id: Post.categories_for_user(user))
end
end
class Post < ApplicationRecord
belongs_to :category
scope :frontend_visible, -> { where(:state => ['approved', 'changes_pending_approval']) }
scope :member_visible, -> { where(:state => ['approved', 'changes_pending_approval', 'private']) }
scope :categories_for_user, -> (user) do
(user.member? ? member_visible : frontend_visible).distinct.pluck(:category_id)
end
end
Active record query using associated model scope
You might need to do something like:
Food.joins(:requests).merge(Request.where_belongs_to(user))
Rails App: How do I use a simple search method to search through associated models?
Assuming ParticipantDetail belongs_to :participant
, and Participant has_one :participant_detail
, use an INNER JOIN
Participant
.select('participants.*')
.select('participant_details.f_name, participant_details.l_name')
.joins(:participant_details)
.where('participants.email ILIKE :search OR participant_details.f_name ILIKE :search OR participant_details.l_name ILIKE :search', search: terms)
Rails query associated model
Locations for particular region
Location.joins(:region).where(regions: {id: 5})
Related Topics
Calling Instance Variables Without @
Undefined Method 'Email' for Nil:Nilclass in Exibe the Mail of Table Father
How to Specify Formatting Options for To_Yaml in Ruby
Change Emacs Ruby-Mode Indent to 4 Spaces
Custom To_Yaml and Domain_Type
How to Convert This Ruby String into an Array
Ruby: Calculate Time Difference Between 2 Times
Ruby: Why Are All Coordinates Getting Updated in an Array of Arrays
How to Transform the Utf8 Chars to Iso8859-1
Pg_Dump: [Archiver (Db)] Query Failed: Error: Permission Denied for Relation Abouts
Why Does Single '=' Work in 'If' Statement
How to Serialize as Activesupport::Hashwithindifferentaccess Anymore
Error When Starting Sinatra: "Tried to Create Proc Object Without a Block"