Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top