#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" %><br />
    <%= user_group.text_field :name_starts_with %><br />
    <br />
  <% end %>

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

undefined method user_group=' for #<UserConditions>

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

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

Pages