{"action":"created","issue":{"url":"https://api.github.com/repos/pat/thinking-sphinx/issues/969","repository_url":"https://api.github.com/repos/pat/thinking-sphinx","labels_url":"https://api.github.com/repos/pat/thinking-sphinx/issues/969/labels{/name}","comments_url":"https://api.github.com/repos/pat/thinking-sphinx/issues/969/comments","events_url":"https://api.github.com/repos/pat/thinking-sphinx/issues/969/events","html_url":"https://github.com/pat/thinking-sphinx/issues/969","id":142070462,"number":969,"title":"Realtime index deeply nested model associations","user":{"login":"al3izz","id":8162017,"avatar_url":"https://avatars.githubusercontent.com/u/8162017?v=3","gravatar_id":"","url":"https://api.github.com/users/al3izz","html_url":"https://github.com/al3izz","followers_url":"https://api.github.com/users/al3izz/followers","following_url":"https://api.github.com/users/al3izz/following{/other_user}","gists_url":"https://api.github.com/users/al3izz/gists{/gist_id}","starred_url":"https://api.github.com/users/al3izz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/al3izz/subscriptions","organizations_url":"https://api.github.com/users/al3izz/orgs","repos_url":"https://api.github.com/users/al3izz/repos","events_url":"https://api.github.com/users/al3izz/events{/privacy}","received_events_url":"https://api.github.com/users/al3izz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-03-19T15:20:51Z","updated_at":"2016-03-23T04:38:18Z","closed_at":null,"body":"By far, I have found Thinking Sphinx and Sphinx Search to be the best searching APIs I've ever used. I'm really glad with the work which has been done.\r\n\r\nHowever, I'm still a bit confused about a couple of things regarding Realtime indexing:\r\n\r\nSay I have the following association relationship:\r\n\r\n```\r\n# Person.rb\r\n\r\nafter_save ThinkingSphinx::RealTime.callback_for(:person, [:person])\r\n\r\nhas_many :oldjobs, dependent: :destroy\r\nhas_many :experiences, dependent: :destroy\r\n\r\n\r\n# Experience.rb\r\n\r\nbelongs_to :person, touch: true\r\nbelongs_to :department\r\n\r\n\r\n# Oldjob.rb\r\n\r\nbelongs_to :person, touch: true\r\n```\r\n\r\nSo now, the questions:\r\n\r\n**1. How do I index something like Person.experiences.department.name ???**\r\nI read that I have to use a method inside Person.rb, so I tried the following **but it doesn't work**:\r\n\r\n\r\n```\r\n# Person.rb\r\n\r\ndef experiences_department_names\r\n  experiences.map{ |e| e.department.name }.join(' ')\r\nend\r\n```\r\n\r\n```\r\n# person_index.rb\r\n\r\nindexes experiences_department_names\r\n```\r\n\r\n\r\n**2. How do I correctly register the after_save callbacks for the above relationship?**\r\nSo far I only put one inside Person.rb, but I'm unsure what to do about the other models.\r\n\r\n\r\n**3. How do I save my realtime indices?** I find that they disappear if I restart the server and I end up having to re-index. And when I check inside my db/sphinx/environment folder, I don't find any index files, it's just a blank folder.\r\n\r\n\r\n**4. Are destroy callbacks already incorporated for Realtime indices?** For example, if I delete a model instance, it would be removed from the sphinx index automatically right?\r\n\r\n\r\nThank you."},"comment":{"url":"https://api.github.com/repos/pat/thinking-sphinx/issues/comments/200170605","html_url":"https://github.com/pat/thinking-sphinx/issues/969#issuecomment-200170605","issue_url":"https://api.github.com/repos/pat/thinking-sphinx/issues/969","id":200170605,"user":{"login":"pat","id":4183,"avatar_url":"https://avatars.githubusercontent.com/u/4183?v=3","gravatar_id":"","url":"https://api.github.com/users/pat","html_url":"https://github.com/pat","followers_url":"https://api.github.com/users/pat/followers","following_url":"https://api.github.com/users/pat/following{/other_user}","gists_url":"https://api.github.com/users/pat/gists{/gist_id}","starred_url":"https://api.github.com/users/pat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pat/subscriptions","organizations_url":"https://api.github.com/users/pat/orgs","repos_url":"https://api.github.com/users/pat/repos","events_url":"https://api.github.com/users/pat/events{/privacy}","received_events_url":"https://api.github.com/users/pat/received_events","type":"User","site_admin":false},"created_at":"2016-03-23T04:38:18Z","updated_at":"2016-03-23T04:38:18Z","body":"1A: Are you finding the method you've added to your model is returning the right data, even though Sphinx isn't?\r\n\r\n1B: Are you using both SQL-backed and real-time indices? If not, you don't need to use the `ts:rebuild` task. That said, manually stopping Sphinx before running `rake ts:regenerate` might be more reliable.\r\n\r\n2A: The callback you've suggested in your Person model to cover the Department index is indeed correct.\r\n\r\n2B: This callback isn't valid - it would be hitting the `people` method on a department instance, and treating each person instance as a department instance and trying to save department index values from the person object.\r\n\r\nIf you want to have the person index update when a department is updated, you'd instead want the following:\r\n\r\n```ruby\r\nafter_save ThinkingSphinx::RealTime.callback_for(:person, [:people])\r\n```\r\n\r\n3: That code is only useful when you want the index files to be updated before copying data elsewhere (so, possibly in a rake task called before backing up data?). It's not needed in an initializer. Sphinx should persist data automatically, it just won't appear _immediately_ in the file system while Sphinx is running."}}