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









up vote
0
down vote

favorite












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

























    up vote
    0
    down vote

    favorite












    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























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 at 1:00









      marcamillion

      10.8k43137287




      10.8k43137287






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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 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 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 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',
          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








          up vote
          2
          down vote



          accepted










          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 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 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 at 1:22














          up vote
          2
          down vote



          accepted










          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 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 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 at 1:22












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          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 at 1:37

























          answered Nov 12 at 1:06









          Marcin Kołodziej

          4,067315




          4,067315











          • 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 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 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 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 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 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 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 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 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 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 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 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 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3