4 Replies Latest reply on Jul 10, 2013 10:58 AM by Pietro Francesco Maggi

    Ruby idiot question...

    David Miller

      Been pulling my hair out trying to figure out the ruby syntax for conditionally checking a data item in a propertybag model.

       

      Model:

      ApplicationData

       

      Attributes:

      data1 => "1"

      data2 => "A"

      data3 => "Description of something"

       

      def check_model_attribute

           @appdata = ApplicationData.find(:first)

           if @appdata.data1 == "1"

                render some view

           else

                render another view

           end

      end

       

      I don't know the syntax for the if @appdata.data1 == "1" line above.  Tried...

      if @appdata.["data1"]

      if @appdata.[:data1]

      if @appdata.data1

      if @appdata[0].data1

      == "1"

      == '1'

      ...and permutations thereof.

       

      Can anyone offer the correct syntax for accessing model attributes for testing using conditionals?

       

      Thx

        • Re: Ruby idiot question...
          Marcus Atkins

          @appdata.data1 is correct for accessing the value.

           

          Check that @appdata = ApplicationData.find(:first) is actually returning a record with that attribute.

            • Re: Ruby idiot question...
              David Miller

              The debug shows the data is being returned and set to the value of "1".

               

              But when continuing the trace, the logic just isn't being followed to 'render some view'.

               

              So...

               

              def check_model_attribute                       

                   @appdata = ApplicationData.find(:first)                    <------returns the correct data

                   if @appdata.data1 == "1"                                          <------should evaluate to true

                        render some view                                                 <------doesn't run this

                   else

                        render another view                                             <------instead runs this

                   end

              end

               

              Arrrgh.

                • Re: Ruby idiot question...
                  Marcus Atkins

                  def check_model_attribute                      

                       @appdata = ApplicationData.find(:first)                   

                       if @appdata.data1 == "1"                                <----- Set a break point on this line

                            render some view                                               

                       else

                            render another view                                            

                       end

                  end

                   

                  If you are using RhoStudio (eclipse based) copy @appdata.data1 into the expressions tab to evaluate it. Does this return "1"? If so, evaluate '@appdata.data1 == "1" ', does this return true?

                   

                  Are you able to send me a sample app?

                  • Re: Ruby idiot question...
                    Pietro Francesco Maggi

                    Hi David,

                    I've built a simple test app with RMS v2.2SP1 and the logic it's executed, only problem was to use redirect instead of render:

                     

                      # GET /ApplicationData/check_model_attribute

                      def check_model_attribute

                           @appdata = ApplicationData.find(:first)

                           if @appdata.data1 == "1"

                             redirect :action => :index

                           else

                             redirect :action => :new

                           end

                      end

                     

                    Attached a small sample app using this. Just create a record, in the show view you've a button to recall this action.

                     

                    ~Pietro