Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8653013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:26:26+00:00 2026-06-12T14:26:26+00:00

I had a problem where I was getting duplicate results in a search, in

  • 0

I had a problem where I was getting duplicate results in a search, in my rails app that allow the user to search for projects in a database.

Here is the search function in the project model:

def self.search(search_industry, search_role, search_techs_ids)

    _projects = Project.scoped 

    if search_industry.present?
      _projects = _projects.where ['industry LIKE ?', like(search_industry)]
    end
    if search_role.present?
      _projects = _projects.where ['role LIKE ?', like(search_role)]
    end
    if search_techs_ids.present?
    _projects = _projects.includes(:technols).where("technols.id" => search_techs_ids)
    end
    _projects
    end

and here is part of my search page

<div class="tech">
<%= fields_for(@project_technol) do |ab| %>
 Technologies : 
 <%     tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil? %>

<%if params[:technols].nil?%>

<%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true} ) %>

<% else %>

<%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true, :selected => tech_ids } ) %>
<% end %>

</div>

Here is my search action:

def search

    tech_ids = params[:technols][:id].reject(&:blank?) unless params[:technols].nil?

    @search =  params[:industry], params[:role], tech_ids

    @project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)

    @search_performed = !@search.reject! { |c| c.blank? }.empty? 

  @project = Project.new(params[:project])

    @all_technols = Technol.all

    @project_technol = @project.projecttechnols.build

respond_to do |format|
      format.html # search.html.erb
      format.json { render :json => @project }
    end

end

That problem is now sorted, but in fixing that, I noticed that when I search for a single technology, the correct projects show up in the table, but in the technology column it is meant to show all the technologies belonging to that project but it only shows the one I searched for:

<% @project_search.each do |t| %>
  <tr>
    <td><ul>
      <% t.technols.each do |technol| %>
        <li><%= technol.tech %><!/li>
      <% end %>
    </ul></td>

Any ideas? In my model I used to have this code which worked for displaying the table correctly, but showed duplicate results.

if search_techs_ids.present?
        _projects = _projects.joins(:technols).where("technols.id" => search_techs_ids)
        end

Any help at all will be appreciated. Thanks in advance

SOLUTION see answers below

@shioyama and @tommasop both helped fix the problem.

The solution was to change my search view where the column is made to

<td><ul>
  <% t.technols(force_reload=true).each do |technol| %>
    <li><%= technol.tech %></li>

  <% end %>

</ul></td>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:26:27+00:00Added an answer on June 12, 2026 at 2:26 pm

    I encountered the same problem in a different guise: Matching nested model association attribute with includes

    See also this discussion on the globalize3 github issue page, where the problem came up when trying to destroy a record after searching by dynamic finder (leaving unmatched associations behind in the DB).

    The problem is that you’re restricting the join to projects with given technology associations (those with ids in search_tech_ids), so those are the only rows that rails returns. Rails has no way of knowing there are others there.

    Basically so far as I can tell, the easiest solution is just to reload the projects after completing the search. Add a line just above the last line of your search method, like so:

    _projects.each { |p| p.technols.reload }
    _projects
    

    This will tell rails to reload the technologies associated with each project, including the technologies it did not match in the search. I believe that should work in your case.

    UPDATE:

    After many attempts (see comments) and a suggestion from @tommasop, the solution was to force-reload the technologies in the view with:

    t.technols(force_reload=true).each
    

    Still not clear why the reload approach above wasn’t working. If anyone has any insights please let me know, I’d be curious to hear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I had a problem with getting the data from the Database, it took
I had a problem to add a file association that was resolved here ,
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I had a problem with some JavaScript functions that had me scratching my head
I had a problem a couple of days ago where I was getting a
I had made one application in java-swing, Now what i am getting problem is,
Here I had a problem with the date formatters actually I had an option
I had a problem getting a VB.NET web service client to receive the response
I'm having a problem getting the root URL with this PHP function I composed.
I have never had a problem getting Facebook to show thumbnails for shared links

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.