0

I'm using Sunspot Solr to search events and each event has a group_id that refers to a Group object (multiple events can have the same group). I want to find the right events if the user searches the group name.

Attempted solution in the searchable block

string :events_group_name do
  group.map(&:name)
end

Error

SQLite3::SQLException: no such column: groups.event_id: SELECT "groups".* FROM "groups" WHERE "groups"."event_id" = 3 LIMIT 1

Problem is that there is no event_id in Group, so how can I get this to work? The workaround is to save the group name as a column in the Event object, but surely there's a better way. Thank you!

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
Andrew
  • 1,167
  • 1
  • 10
  • 16

1 Answers1

0

I don't know anything about Sunspot, but I believe that your model is incorrectly mapped. If you have a association like:

1 Group -> N events

You should use a has_many and a belongs_to in your model. has_one is only used with associations 1 to 1. In your case:

class Group
  has_many :events
  #...
end

class Event
  belongs_to :group
  #...
end

The Rails Guide has a good article about Active Record associations with examples. You can take a look at it.

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
  • 1
    Thanks a ton! I figured it out, first we change it to belongs_to as per your suggestion (stupid stupid me) and then in the searchable block add, text :group do group.name if group end (to check if group is nil) – Andrew Mar 11 '13 at 19:23