Rails with Searchkick: getting results with a joined table












0















I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).



House:



class House < ApplicationRecord
searchkick
has_one :address, dependent: :destroy
end


Address:



class Address < ApplicationRecord
searchkick
belongs_to :house
end


In my controller I have



def index
if params[:term].present?
@houses = House.search(params[:term])
else
@houses = House.search('*')
end
end


Right now, searchkick is working when I search for house data which is in the table houses, but if for example I look for a field in the Addresses table, I do not get any result at all.
Is it possible to do this?



Thanks a lot in advance!










share|improve this question





























    0















    I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).



    House:



    class House < ApplicationRecord
    searchkick
    has_one :address, dependent: :destroy
    end


    Address:



    class Address < ApplicationRecord
    searchkick
    belongs_to :house
    end


    In my controller I have



    def index
    if params[:term].present?
    @houses = House.search(params[:term])
    else
    @houses = House.search('*')
    end
    end


    Right now, searchkick is working when I search for house data which is in the table houses, but if for example I look for a field in the Addresses table, I do not get any result at all.
    Is it possible to do this?



    Thanks a lot in advance!










    share|improve this question



























      0












      0








      0


      1






      I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).



      House:



      class House < ApplicationRecord
      searchkick
      has_one :address, dependent: :destroy
      end


      Address:



      class Address < ApplicationRecord
      searchkick
      belongs_to :house
      end


      In my controller I have



      def index
      if params[:term].present?
      @houses = House.search(params[:term])
      else
      @houses = House.search('*')
      end
      end


      Right now, searchkick is working when I search for house data which is in the table houses, but if for example I look for a field in the Addresses table, I do not get any result at all.
      Is it possible to do this?



      Thanks a lot in advance!










      share|improve this question
















      I'm starting to use searchkick in my app, and I have two models: House and Address (which belongs to a house).



      House:



      class House < ApplicationRecord
      searchkick
      has_one :address, dependent: :destroy
      end


      Address:



      class Address < ApplicationRecord
      searchkick
      belongs_to :house
      end


      In my controller I have



      def index
      if params[:term].present?
      @houses = House.search(params[:term])
      else
      @houses = House.search('*')
      end
      end


      Right now, searchkick is working when I search for house data which is in the table houses, but if for example I look for a field in the Addresses table, I do not get any result at all.
      Is it possible to do this?



      Thanks a lot in advance!







      ruby-on-rails elasticsearch searchkick






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 10:27







      noloman

















      asked Nov 24 '18 at 16:34









      nolomannoloman

      5,344136393




      5,344136393
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I think you need to index addresses also. To do that add the below code to your House model:



          def search_data
          attributes.merge(
          address_city: self.address.try(:city),
          address_zip: self.address.try(:zip)
          )
          end


          Feel free to modify the above code based on your fields in the Address model.



          Also, after adding the above code, don't forget to reindex.






          share|improve this answer


























          • well it isn't really working: undefined local variable or method addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class

            – noloman
            Nov 25 '18 at 10:20











          • That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your House model.

            – Abhilash Reddy
            Nov 25 '18 at 10:22











          • and also the columns you've in the other table.

            – Abhilash Reddy
            Nov 25 '18 at 10:23











          • I edited my question with the models, thanks a lot!

            – noloman
            Nov 25 '18 at 10:27











          • @noloman Updated the answer. Since you've not provided the fields available in address model, I've assumed that you will have city and zip in addresses table

            – Abhilash Reddy
            Nov 25 '18 at 10:38











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460192%2frails-with-searchkick-getting-results-with-a-joined-table%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I think you need to index addresses also. To do that add the below code to your House model:



          def search_data
          attributes.merge(
          address_city: self.address.try(:city),
          address_zip: self.address.try(:zip)
          )
          end


          Feel free to modify the above code based on your fields in the Address model.



          Also, after adding the above code, don't forget to reindex.






          share|improve this answer


























          • well it isn't really working: undefined local variable or method addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class

            – noloman
            Nov 25 '18 at 10:20











          • That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your House model.

            – Abhilash Reddy
            Nov 25 '18 at 10:22











          • and also the columns you've in the other table.

            – Abhilash Reddy
            Nov 25 '18 at 10:23











          • I edited my question with the models, thanks a lot!

            – noloman
            Nov 25 '18 at 10:27











          • @noloman Updated the answer. Since you've not provided the fields available in address model, I've assumed that you will have city and zip in addresses table

            – Abhilash Reddy
            Nov 25 '18 at 10:38
















          0














          I think you need to index addresses also. To do that add the below code to your House model:



          def search_data
          attributes.merge(
          address_city: self.address.try(:city),
          address_zip: self.address.try(:zip)
          )
          end


          Feel free to modify the above code based on your fields in the Address model.



          Also, after adding the above code, don't forget to reindex.






          share|improve this answer


























          • well it isn't really working: undefined local variable or method addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class

            – noloman
            Nov 25 '18 at 10:20











          • That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your House model.

            – Abhilash Reddy
            Nov 25 '18 at 10:22











          • and also the columns you've in the other table.

            – Abhilash Reddy
            Nov 25 '18 at 10:23











          • I edited my question with the models, thanks a lot!

            – noloman
            Nov 25 '18 at 10:27











          • @noloman Updated the answer. Since you've not provided the fields available in address model, I've assumed that you will have city and zip in addresses table

            – Abhilash Reddy
            Nov 25 '18 at 10:38














          0












          0








          0







          I think you need to index addresses also. To do that add the below code to your House model:



          def search_data
          attributes.merge(
          address_city: self.address.try(:city),
          address_zip: self.address.try(:zip)
          )
          end


          Feel free to modify the above code based on your fields in the Address model.



          Also, after adding the above code, don't forget to reindex.






          share|improve this answer















          I think you need to index addresses also. To do that add the below code to your House model:



          def search_data
          attributes.merge(
          address_city: self.address.try(:city),
          address_zip: self.address.try(:zip)
          )
          end


          Feel free to modify the above code based on your fields in the Address model.



          Also, after adding the above code, don't forget to reindex.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 10:46

























          answered Nov 24 '18 at 17:48









          Abhilash ReddyAbhilash Reddy

          1,1401618




          1,1401618













          • well it isn't really working: undefined local variable or method addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class

            – noloman
            Nov 25 '18 at 10:20











          • That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your House model.

            – Abhilash Reddy
            Nov 25 '18 at 10:22











          • and also the columns you've in the other table.

            – Abhilash Reddy
            Nov 25 '18 at 10:23











          • I edited my question with the models, thanks a lot!

            – noloman
            Nov 25 '18 at 10:27











          • @noloman Updated the answer. Since you've not provided the fields available in address model, I've assumed that you will have city and zip in addresses table

            – Abhilash Reddy
            Nov 25 '18 at 10:38



















          • well it isn't really working: undefined local variable or method addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class

            – noloman
            Nov 25 '18 at 10:20











          • That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your House model.

            – Abhilash Reddy
            Nov 25 '18 at 10:22











          • and also the columns you've in the other table.

            – Abhilash Reddy
            Nov 25 '18 at 10:23











          • I edited my question with the models, thanks a lot!

            – noloman
            Nov 25 '18 at 10:27











          • @noloman Updated the answer. Since you've not provided the fields available in address model, I've assumed that you will have city and zip in addresses table

            – Abhilash Reddy
            Nov 25 '18 at 10:38

















          well it isn't really working: undefined local variable or method addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class

          – noloman
          Nov 25 '18 at 10:20





          well it isn't really working: undefined local variable or method addresses' for #<Class:0x00007fb78071ddd0>`, how can I do that? I didn't know I could use other models inside a model class

          – noloman
          Nov 25 '18 at 10:20













          That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your House model.

          – Abhilash Reddy
          Nov 25 '18 at 10:22





          That was just an example. To give you an exact answer, You will have to provide us with the associations you have defined in your House model.

          – Abhilash Reddy
          Nov 25 '18 at 10:22













          and also the columns you've in the other table.

          – Abhilash Reddy
          Nov 25 '18 at 10:23





          and also the columns you've in the other table.

          – Abhilash Reddy
          Nov 25 '18 at 10:23













          I edited my question with the models, thanks a lot!

          – noloman
          Nov 25 '18 at 10:27





          I edited my question with the models, thanks a lot!

          – noloman
          Nov 25 '18 at 10:27













          @noloman Updated the answer. Since you've not provided the fields available in address model, I've assumed that you will have city and zip in addresses table

          – Abhilash Reddy
          Nov 25 '18 at 10:38





          @noloman Updated the answer. Since you've not provided the fields available in address model, I've assumed that you will have city and zip in addresses table

          – Abhilash Reddy
          Nov 25 '18 at 10:38




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460192%2frails-with-searchkick-getting-results-with-a-joined-table%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin