Rails associations with search












0















I have issue with associations with search



class Manufacturer < ApplicationRecord

has_many :translations, class_name: 'ManufacturerTranslation', dependent: :destroy

def self.search(query)
q = query.to_s.parameterize.gsub('-',' ').split
joins(:translations).where("lower(name) LIKE ?", "%#{q}%")
end

and



class ManufacturerTranslation < ApplicationRecord
belongs_to :manufacturer
end


so when i have tried to do search and call translations on it



Manufacturer.search('fra').last.translations


it gives me only translations with name which include fra, not all of translations



so i have 6 translations in total for this manufacturer



but after search a get only 2



database schema



 create_table "manufacturer_translations" do |t|
t.integer "manufacturer_id"
t.string "locale"
t.string "name"
t.string "image_source_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manufacturer_id"], name: "index_manufacturer_translations_on_manufacturer_id"
t.index ["name"], name: "index_manufacturer_translations_on_name"
end

create_table "manufacturers", do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "deleted", default: false
end









share|improve this question

























  • in which table you are trying to search name?

    – wasipeer
    Jan 3 at 13:40











  • @wasipeer Manufacturer example in question

    – user10862870
    Jan 3 at 13:42











  • do you have name field in translations also.

    – wasipeer
    Jan 3 at 13:43











  • @wasipeer added schema file

    – user10862870
    Jan 3 at 13:51











  • please accept the answer if it working fine so other can get benefit from this post.

    – wasipeer
    Jan 3 at 15:21
















0















I have issue with associations with search



class Manufacturer < ApplicationRecord

has_many :translations, class_name: 'ManufacturerTranslation', dependent: :destroy

def self.search(query)
q = query.to_s.parameterize.gsub('-',' ').split
joins(:translations).where("lower(name) LIKE ?", "%#{q}%")
end

and



class ManufacturerTranslation < ApplicationRecord
belongs_to :manufacturer
end


so when i have tried to do search and call translations on it



Manufacturer.search('fra').last.translations


it gives me only translations with name which include fra, not all of translations



so i have 6 translations in total for this manufacturer



but after search a get only 2



database schema



 create_table "manufacturer_translations" do |t|
t.integer "manufacturer_id"
t.string "locale"
t.string "name"
t.string "image_source_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manufacturer_id"], name: "index_manufacturer_translations_on_manufacturer_id"
t.index ["name"], name: "index_manufacturer_translations_on_name"
end

create_table "manufacturers", do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "deleted", default: false
end









share|improve this question

























  • in which table you are trying to search name?

    – wasipeer
    Jan 3 at 13:40











  • @wasipeer Manufacturer example in question

    – user10862870
    Jan 3 at 13:42











  • do you have name field in translations also.

    – wasipeer
    Jan 3 at 13:43











  • @wasipeer added schema file

    – user10862870
    Jan 3 at 13:51











  • please accept the answer if it working fine so other can get benefit from this post.

    – wasipeer
    Jan 3 at 15:21














0












0








0








I have issue with associations with search



class Manufacturer < ApplicationRecord

has_many :translations, class_name: 'ManufacturerTranslation', dependent: :destroy

def self.search(query)
q = query.to_s.parameterize.gsub('-',' ').split
joins(:translations).where("lower(name) LIKE ?", "%#{q}%")
end

and



class ManufacturerTranslation < ApplicationRecord
belongs_to :manufacturer
end


so when i have tried to do search and call translations on it



Manufacturer.search('fra').last.translations


it gives me only translations with name which include fra, not all of translations



so i have 6 translations in total for this manufacturer



but after search a get only 2



database schema



 create_table "manufacturer_translations" do |t|
t.integer "manufacturer_id"
t.string "locale"
t.string "name"
t.string "image_source_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manufacturer_id"], name: "index_manufacturer_translations_on_manufacturer_id"
t.index ["name"], name: "index_manufacturer_translations_on_name"
end

create_table "manufacturers", do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "deleted", default: false
end









share|improve this question
















I have issue with associations with search



class Manufacturer < ApplicationRecord

has_many :translations, class_name: 'ManufacturerTranslation', dependent: :destroy

def self.search(query)
q = query.to_s.parameterize.gsub('-',' ').split
joins(:translations).where("lower(name) LIKE ?", "%#{q}%")
end

and



class ManufacturerTranslation < ApplicationRecord
belongs_to :manufacturer
end


so when i have tried to do search and call translations on it



Manufacturer.search('fra').last.translations


it gives me only translations with name which include fra, not all of translations



so i have 6 translations in total for this manufacturer



but after search a get only 2



database schema



 create_table "manufacturer_translations" do |t|
t.integer "manufacturer_id"
t.string "locale"
t.string "name"
t.string "image_source_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manufacturer_id"], name: "index_manufacturer_translations_on_manufacturer_id"
t.index ["name"], name: "index_manufacturer_translations_on_name"
end

create_table "manufacturers", do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "deleted", default: false
end






ruby-on-rails associations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 13:50







user10862870

















asked Jan 3 at 13:33









user10862870user10862870

11




11













  • in which table you are trying to search name?

    – wasipeer
    Jan 3 at 13:40











  • @wasipeer Manufacturer example in question

    – user10862870
    Jan 3 at 13:42











  • do you have name field in translations also.

    – wasipeer
    Jan 3 at 13:43











  • @wasipeer added schema file

    – user10862870
    Jan 3 at 13:51











  • please accept the answer if it working fine so other can get benefit from this post.

    – wasipeer
    Jan 3 at 15:21



















  • in which table you are trying to search name?

    – wasipeer
    Jan 3 at 13:40











  • @wasipeer Manufacturer example in question

    – user10862870
    Jan 3 at 13:42











  • do you have name field in translations also.

    – wasipeer
    Jan 3 at 13:43











  • @wasipeer added schema file

    – user10862870
    Jan 3 at 13:51











  • please accept the answer if it working fine so other can get benefit from this post.

    – wasipeer
    Jan 3 at 15:21

















in which table you are trying to search name?

– wasipeer
Jan 3 at 13:40





in which table you are trying to search name?

– wasipeer
Jan 3 at 13:40













@wasipeer Manufacturer example in question

– user10862870
Jan 3 at 13:42





@wasipeer Manufacturer example in question

– user10862870
Jan 3 at 13:42













do you have name field in translations also.

– wasipeer
Jan 3 at 13:43





do you have name field in translations also.

– wasipeer
Jan 3 at 13:43













@wasipeer added schema file

– user10862870
Jan 3 at 13:51





@wasipeer added schema file

– user10862870
Jan 3 at 13:51













please accept the answer if it working fine so other can get benefit from this post.

– wasipeer
Jan 3 at 15:21





please accept the answer if it working fine so other can get benefit from this post.

– wasipeer
Jan 3 at 15:21












2 Answers
2






active

oldest

votes


















0














Assuming you follow Rails conventions I'd suggest you to replace this



joins(:translations).where("lower(name) LIKE ?", "%#{q}%")



with



joins(:translations).where("lower(manufacturers.name) LIKE ?", "%#{q}%")



May be needed to play with quotations






share|improve this answer































    0














    As you are splitting the string you have to loop through all the entities to search them. And you also have to downcase the results. try out the following code:



    def self.search(query)
    q = query.to_s.parameterize.gsub('-',' ').split
    conditions = ''
    q.each do |qu|
    conditions = "#{conditions} OR " if conditions.present?
    conditions = "#{condidtions} lower(manufacturers.name) LIKE %#{qu.downcase}%"
    end

    joins(:translations).where(conditions)
    end





    share|improve this answer
























      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%2f54023313%2frails-associations-with-search%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Assuming you follow Rails conventions I'd suggest you to replace this



      joins(:translations).where("lower(name) LIKE ?", "%#{q}%")



      with



      joins(:translations).where("lower(manufacturers.name) LIKE ?", "%#{q}%")



      May be needed to play with quotations






      share|improve this answer




























        0














        Assuming you follow Rails conventions I'd suggest you to replace this



        joins(:translations).where("lower(name) LIKE ?", "%#{q}%")



        with



        joins(:translations).where("lower(manufacturers.name) LIKE ?", "%#{q}%")



        May be needed to play with quotations






        share|improve this answer


























          0












          0








          0







          Assuming you follow Rails conventions I'd suggest you to replace this



          joins(:translations).where("lower(name) LIKE ?", "%#{q}%")



          with



          joins(:translations).where("lower(manufacturers.name) LIKE ?", "%#{q}%")



          May be needed to play with quotations






          share|improve this answer













          Assuming you follow Rails conventions I'd suggest you to replace this



          joins(:translations).where("lower(name) LIKE ?", "%#{q}%")



          with



          joins(:translations).where("lower(manufacturers.name) LIKE ?", "%#{q}%")



          May be needed to play with quotations







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 13:49









          VADVAD

          1,72131425




          1,72131425

























              0














              As you are splitting the string you have to loop through all the entities to search them. And you also have to downcase the results. try out the following code:



              def self.search(query)
              q = query.to_s.parameterize.gsub('-',' ').split
              conditions = ''
              q.each do |qu|
              conditions = "#{conditions} OR " if conditions.present?
              conditions = "#{condidtions} lower(manufacturers.name) LIKE %#{qu.downcase}%"
              end

              joins(:translations).where(conditions)
              end





              share|improve this answer




























                0














                As you are splitting the string you have to loop through all the entities to search them. And you also have to downcase the results. try out the following code:



                def self.search(query)
                q = query.to_s.parameterize.gsub('-',' ').split
                conditions = ''
                q.each do |qu|
                conditions = "#{conditions} OR " if conditions.present?
                conditions = "#{condidtions} lower(manufacturers.name) LIKE %#{qu.downcase}%"
                end

                joins(:translations).where(conditions)
                end





                share|improve this answer


























                  0












                  0








                  0







                  As you are splitting the string you have to loop through all the entities to search them. And you also have to downcase the results. try out the following code:



                  def self.search(query)
                  q = query.to_s.parameterize.gsub('-',' ').split
                  conditions = ''
                  q.each do |qu|
                  conditions = "#{conditions} OR " if conditions.present?
                  conditions = "#{condidtions} lower(manufacturers.name) LIKE %#{qu.downcase}%"
                  end

                  joins(:translations).where(conditions)
                  end





                  share|improve this answer













                  As you are splitting the string you have to loop through all the entities to search them. And you also have to downcase the results. try out the following code:



                  def self.search(query)
                  q = query.to_s.parameterize.gsub('-',' ').split
                  conditions = ''
                  q.each do |qu|
                  conditions = "#{conditions} OR " if conditions.present?
                  conditions = "#{condidtions} lower(manufacturers.name) LIKE %#{qu.downcase}%"
                  end

                  joins(:translations).where(conditions)
                  end






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 14:00









                  wasipeerwasipeer

                  709717




                  709717






























                      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%2f54023313%2frails-associations-with-search%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

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'