#85 ✓resolved
John Felix

searching across deep relationships

Reported by John Felix | May 4th, 2009 @ 04:43 PM

I think this is probably user error, rather than an actual bug, but I can't seem to figure it out. I need to search based on values in deeply nested tables. I'll illustrate this issue using your tutorial example app.

Suppose that we have a need to sub-divide user_groups. We can create a user_sub_group model, and set up a belongs to/has many relationship between it and user_group. It would then make more sense to relate users to user_sub_group, rather than user_group. The modified models would be:

class UserGroup < ActiveRecord::Base
has_many :user_sub_groups end

class UserSubGroup < ActiveRecord::Base
has_many :users belongs_to :user_group end

class User < ActiveRecord::Base
belongs_to :user_sub_group has_many :orders, :dependent => :destroy

def name(middle_name = "") "#{first_name} #{middle_name} #{last_name}" end end

For this example, I'm leaving user_groups as the display column in the view, but I cant get this easily enough. I also want to still be able to search by user_group. It looks like I should be able to adjust that search block to look like:

<% users.fields_for users.object.user_sub_group.user_group do |user_group| %>
<%= user_group.label :name_starts_with, "Belongs to user group with name that starts with" %>
<%= user_group.text_field :name_starts_with %>

<% end %>

However, when I try to submit a search using the above view code, I receive the following error:

undefined method `user_group=' for #

Is this an actual bug, or (more likely) am I just setting it up wrong? How does one set up the form so that it will search fields in related models that are more than one level removed from the main search model? Any help you can provide will be greatly appreciated.

Comments and changes to this ticket

  • Ben Johnson

    Ben Johnson June 3rd, 2009 @ 04:06 AM

    • State changed from “new” to “open”

    The above should work, all that searchlogic is doing is building options to pass to AR, its not actually building the whole query. Can you get this to work without using searchlogic, just using find and good old sql? Because if you can do that, I am fairly confident searchlogic can do it. Assuming you are using the :include or :joins utility that AR provides and not writing manual joins.

  • Ben Johnson

    Ben Johnson July 19th, 2009 @ 01:29 PM

    • State changed from “open” to “resolved”

    Let me know if you have anymore issues, I couldn't reproduce this. Thanks.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Provides common named scopes and object based searching.

People watching this ticket