How do I search an entire model based on the value returned by a virtual attribute on that model?










0















I have a model Property, and I have a virtual attribute that is defined like this:



 def uid_type
if mls? && mls.to_i != 0
"MLS"
elsif property_identifier? && property_identifier.to_i != 0
"PID"
else
"ID"
end
end


Such that if I have a property p, when I query that virtual attribute, this is what I see:



> p.uid_type
=> "MLS"


Basically, what I want to do is create a scope on my model to return all properties that have uid_type == 'MLS'.



How do I do that?



Edit 1



If I try this:



Property.where('properties.uid_type == "MLS"').count
(4.6ms) SELECT COUNT(*) FROM "properties" WHERE (properties.uid_type == "MLS")
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column properties.uid_type does not exist
LINE 1: SELECT COUNT(*) FROM "properties" WHERE (properties.uid_typ...









share|improve this question


























    0















    I have a model Property, and I have a virtual attribute that is defined like this:



     def uid_type
    if mls? && mls.to_i != 0
    "MLS"
    elsif property_identifier? && property_identifier.to_i != 0
    "PID"
    else
    "ID"
    end
    end


    Such that if I have a property p, when I query that virtual attribute, this is what I see:



    > p.uid_type
    => "MLS"


    Basically, what I want to do is create a scope on my model to return all properties that have uid_type == 'MLS'.



    How do I do that?



    Edit 1



    If I try this:



    Property.where('properties.uid_type == "MLS"').count
    (4.6ms) SELECT COUNT(*) FROM "properties" WHERE (properties.uid_type == "MLS")
    ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column properties.uid_type does not exist
    LINE 1: SELECT COUNT(*) FROM "properties" WHERE (properties.uid_typ...









    share|improve this question
























      0












      0








      0








      I have a model Property, and I have a virtual attribute that is defined like this:



       def uid_type
      if mls? && mls.to_i != 0
      "MLS"
      elsif property_identifier? && property_identifier.to_i != 0
      "PID"
      else
      "ID"
      end
      end


      Such that if I have a property p, when I query that virtual attribute, this is what I see:



      > p.uid_type
      => "MLS"


      Basically, what I want to do is create a scope on my model to return all properties that have uid_type == 'MLS'.



      How do I do that?



      Edit 1



      If I try this:



      Property.where('properties.uid_type == "MLS"').count
      (4.6ms) SELECT COUNT(*) FROM "properties" WHERE (properties.uid_type == "MLS")
      ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column properties.uid_type does not exist
      LINE 1: SELECT COUNT(*) FROM "properties" WHERE (properties.uid_typ...









      share|improve this question














      I have a model Property, and I have a virtual attribute that is defined like this:



       def uid_type
      if mls? && mls.to_i != 0
      "MLS"
      elsif property_identifier? && property_identifier.to_i != 0
      "PID"
      else
      "ID"
      end
      end


      Such that if I have a property p, when I query that virtual attribute, this is what I see:



      > p.uid_type
      => "MLS"


      Basically, what I want to do is create a scope on my model to return all properties that have uid_type == 'MLS'.



      How do I do that?



      Edit 1



      If I try this:



      Property.where('properties.uid_type == "MLS"').count
      (4.6ms) SELECT COUNT(*) FROM "properties" WHERE (properties.uid_type == "MLS")
      ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column properties.uid_type does not exist
      LINE 1: SELECT COUNT(*) FROM "properties" WHERE (properties.uid_typ...






      ruby-on-rails activerecord ruby-on-rails-3.2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 1:00









      marcamillionmarcamillion

      11.1k43139290




      11.1k43139290






















          1 Answer
          1






          active

          oldest

          votes


















          2














          You have to write your own scope, ActiveRecord doesn't help much with custom methods.



          scope :with_mls_uid_type, -> where.not(mls: [nil, '']) 


          Which will translate to:



          SELECT "properties".* 
          FROM "properties"
          WHERE ("properties"."mls" IS NOT NULL)
          AND ("properties"."mls" != '')


          If your tag is correct and you're still on Rails 3.2 then you won't have .not and you will have to do:



          where("mls IS NOT NULL AND mls != ''")





          share|improve this answer

























          • I was just about to ask about Rails 3.2 and the not. So I tried your example, and I am getting the following error: > Property.where("mls IS NOT NULL AND mls !=0").count (1.3ms) SELECT COUNT(*) FROM "properties" WHERE (mls IS NOT NULL AND mls !=0) ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying <> integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

            – marcamillion
            Nov 12 '18 at 1:11











          • Btw. are you sure you know how mls.to_i != 0 works if mls is a string? "5foo".to_i == 5, but "foo".to_i == 0. It's important for the scope as it's not as simple to write in PostgreSQL and might not be exactly what you're looking for.

            – Marcin Kołodziej
            Nov 12 '18 at 1:16












          • I am honestly not sure. This is a legacy codebase I am taking over, so I am just now digging through everything. But I will look into that and do proper error handling as needed. Thanks for the answer and the feedback!

            – marcamillion
            Nov 12 '18 at 1:22










          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%2f53254790%2fhow-do-i-search-an-entire-model-based-on-the-value-returned-by-a-virtual-attribu%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









          2














          You have to write your own scope, ActiveRecord doesn't help much with custom methods.



          scope :with_mls_uid_type, -> where.not(mls: [nil, '']) 


          Which will translate to:



          SELECT "properties".* 
          FROM "properties"
          WHERE ("properties"."mls" IS NOT NULL)
          AND ("properties"."mls" != '')


          If your tag is correct and you're still on Rails 3.2 then you won't have .not and you will have to do:



          where("mls IS NOT NULL AND mls != ''")





          share|improve this answer

























          • I was just about to ask about Rails 3.2 and the not. So I tried your example, and I am getting the following error: > Property.where("mls IS NOT NULL AND mls !=0").count (1.3ms) SELECT COUNT(*) FROM "properties" WHERE (mls IS NOT NULL AND mls !=0) ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying <> integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

            – marcamillion
            Nov 12 '18 at 1:11











          • Btw. are you sure you know how mls.to_i != 0 works if mls is a string? "5foo".to_i == 5, but "foo".to_i == 0. It's important for the scope as it's not as simple to write in PostgreSQL and might not be exactly what you're looking for.

            – Marcin Kołodziej
            Nov 12 '18 at 1:16












          • I am honestly not sure. This is a legacy codebase I am taking over, so I am just now digging through everything. But I will look into that and do proper error handling as needed. Thanks for the answer and the feedback!

            – marcamillion
            Nov 12 '18 at 1:22















          2














          You have to write your own scope, ActiveRecord doesn't help much with custom methods.



          scope :with_mls_uid_type, -> where.not(mls: [nil, '']) 


          Which will translate to:



          SELECT "properties".* 
          FROM "properties"
          WHERE ("properties"."mls" IS NOT NULL)
          AND ("properties"."mls" != '')


          If your tag is correct and you're still on Rails 3.2 then you won't have .not and you will have to do:



          where("mls IS NOT NULL AND mls != ''")





          share|improve this answer

























          • I was just about to ask about Rails 3.2 and the not. So I tried your example, and I am getting the following error: > Property.where("mls IS NOT NULL AND mls !=0").count (1.3ms) SELECT COUNT(*) FROM "properties" WHERE (mls IS NOT NULL AND mls !=0) ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying <> integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

            – marcamillion
            Nov 12 '18 at 1:11











          • Btw. are you sure you know how mls.to_i != 0 works if mls is a string? "5foo".to_i == 5, but "foo".to_i == 0. It's important for the scope as it's not as simple to write in PostgreSQL and might not be exactly what you're looking for.

            – Marcin Kołodziej
            Nov 12 '18 at 1:16












          • I am honestly not sure. This is a legacy codebase I am taking over, so I am just now digging through everything. But I will look into that and do proper error handling as needed. Thanks for the answer and the feedback!

            – marcamillion
            Nov 12 '18 at 1:22













          2












          2








          2







          You have to write your own scope, ActiveRecord doesn't help much with custom methods.



          scope :with_mls_uid_type, -> where.not(mls: [nil, '']) 


          Which will translate to:



          SELECT "properties".* 
          FROM "properties"
          WHERE ("properties"."mls" IS NOT NULL)
          AND ("properties"."mls" != '')


          If your tag is correct and you're still on Rails 3.2 then you won't have .not and you will have to do:



          where("mls IS NOT NULL AND mls != ''")





          share|improve this answer















          You have to write your own scope, ActiveRecord doesn't help much with custom methods.



          scope :with_mls_uid_type, -> where.not(mls: [nil, '']) 


          Which will translate to:



          SELECT "properties".* 
          FROM "properties"
          WHERE ("properties"."mls" IS NOT NULL)
          AND ("properties"."mls" != '')


          If your tag is correct and you're still on Rails 3.2 then you won't have .not and you will have to do:



          where("mls IS NOT NULL AND mls != ''")






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 1:37

























          answered Nov 12 '18 at 1:06









          Marcin KołodziejMarcin Kołodziej

          4,4801315




          4,4801315












          • I was just about to ask about Rails 3.2 and the not. So I tried your example, and I am getting the following error: > Property.where("mls IS NOT NULL AND mls !=0").count (1.3ms) SELECT COUNT(*) FROM "properties" WHERE (mls IS NOT NULL AND mls !=0) ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying <> integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

            – marcamillion
            Nov 12 '18 at 1:11











          • Btw. are you sure you know how mls.to_i != 0 works if mls is a string? "5foo".to_i == 5, but "foo".to_i == 0. It's important for the scope as it's not as simple to write in PostgreSQL and might not be exactly what you're looking for.

            – Marcin Kołodziej
            Nov 12 '18 at 1:16












          • I am honestly not sure. This is a legacy codebase I am taking over, so I am just now digging through everything. But I will look into that and do proper error handling as needed. Thanks for the answer and the feedback!

            – marcamillion
            Nov 12 '18 at 1:22

















          • I was just about to ask about Rails 3.2 and the not. So I tried your example, and I am getting the following error: > Property.where("mls IS NOT NULL AND mls !=0").count (1.3ms) SELECT COUNT(*) FROM "properties" WHERE (mls IS NOT NULL AND mls !=0) ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying <> integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

            – marcamillion
            Nov 12 '18 at 1:11











          • Btw. are you sure you know how mls.to_i != 0 works if mls is a string? "5foo".to_i == 5, but "foo".to_i == 0. It's important for the scope as it's not as simple to write in PostgreSQL and might not be exactly what you're looking for.

            – Marcin Kołodziej
            Nov 12 '18 at 1:16












          • I am honestly not sure. This is a legacy codebase I am taking over, so I am just now digging through everything. But I will look into that and do proper error handling as needed. Thanks for the answer and the feedback!

            – marcamillion
            Nov 12 '18 at 1:22
















          I was just about to ask about Rails 3.2 and the not. So I tried your example, and I am getting the following error: > Property.where("mls IS NOT NULL AND mls !=0").count (1.3ms) SELECT COUNT(*) FROM "properties" WHERE (mls IS NOT NULL AND mls !=0) ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying <> integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

          – marcamillion
          Nov 12 '18 at 1:11





          I was just about to ask about Rails 3.2 and the not. So I tried your example, and I am getting the following error: > Property.where("mls IS NOT NULL AND mls !=0").count (1.3ms) SELECT COUNT(*) FROM "properties" WHERE (mls IS NOT NULL AND mls !=0) ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: character varying <> integer HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

          – marcamillion
          Nov 12 '18 at 1:11













          Btw. are you sure you know how mls.to_i != 0 works if mls is a string? "5foo".to_i == 5, but "foo".to_i == 0. It's important for the scope as it's not as simple to write in PostgreSQL and might not be exactly what you're looking for.

          – Marcin Kołodziej
          Nov 12 '18 at 1:16






          Btw. are you sure you know how mls.to_i != 0 works if mls is a string? "5foo".to_i == 5, but "foo".to_i == 0. It's important for the scope as it's not as simple to write in PostgreSQL and might not be exactly what you're looking for.

          – Marcin Kołodziej
          Nov 12 '18 at 1:16














          I am honestly not sure. This is a legacy codebase I am taking over, so I am just now digging through everything. But I will look into that and do proper error handling as needed. Thanks for the answer and the feedback!

          – marcamillion
          Nov 12 '18 at 1:22





          I am honestly not sure. This is a legacy codebase I am taking over, so I am just now digging through everything. But I will look into that and do proper error handling as needed. Thanks for the answer and the feedback!

          – marcamillion
          Nov 12 '18 at 1:22



















          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%2f53254790%2fhow-do-i-search-an-entire-model-based-on-the-value-returned-by-a-virtual-attribu%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

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế