4 Replies Latest reply on Oct 11, 2012 2:30 PM by Gary Miller

    Ordering data in RhoStudio

      I am using RhoStudio and have created a small app, for learning.  The app has two models and uses property bag.  I have entered data and want to order the data by a field in the model when it's displayed in RhoSimulator.  How and where do I do this? 

       

      I believe the data is stored in a SQLite db and if so can I use an order by clause to get it in the desired order?  Is the SQL code even available to modify or is that a big no no.

       

      Thanks,

       

      Gary

        • Re: Ordering data in RhoStudio
          Robert Galvin

          Gary

           

          Take a look at http://docs.rhomobile.com/rhodes/rhom#rhom-api and scroll down to the order examples section. You may also want to look at the paginate section as well.

           

          Think of RHOM as abstracting the SQLite implementation below. There is a way to use straight SQL, but often is easier just to use the RHOM apis for typical DB operations.

          1 of 1 people found this helpful
            • Re: Ordering data in RhoStudio

              Robert,

               

              Thanks for the response.  I've looked over the info you suggested but I'm still a foggy on how do implement it.  I've been reviewing your presentation from the Appforum Americas 2012, entitled RhoStudio Introduction to Building Native Apps and would like to use that as an example.

               

              If I wanted to sort / order the Product Catalog by the productcatalog.Name how would I do that?

              Below is the index.erb from the Product Catalog folder from your example.  I'm assuming this is where I would do the order by.  I've tried a few different ways to insert the :order from the rhom-api that you pointed me to, but all have resulted in errors when I try to load the page.

               

              <div data-role="page">

               

              <div data-role="header" data-position="inline">

              <h1>ProductCatalogs</h1>

                  <a href="<%= Rho::RhoConfig.start_path %>" class="ui-btn-left" data-icon="home" data-direction="reverse" <%= "data-ajax='false'" if is_bb6 %>>

              Home

              </a>

                  <a href="<%= url_for :action => :new %>" class="ui-btn-right" data-icon="plus">

              New

              </a>

              </div>

               

                <div data-role="content">

              <ul data-role="listview">

              <% @productcatalogs.each do |productcatalog| %>

                   

              <li>

              <a href="<%= url_for :action => :show, :id => productcatalog.object %>">

              <%= productcatalog.Name %>

              </a>

              </li>

                   

              <% end %>

              </ul>

              </div>

               

              </div>

               

              Thanks again for your help.

               

              Gary

                • Re: Ordering data in RhoStudio
                  Jon Tara

                  You do it wherever you retrieve the data from the database. In your example above, then, it would be in the Controller, where you create @productcatalogs.

                   

                  For example, the generated boilerplate will give you:

                   

                  def index

                    @productcatalogs = ProductCatalog.find(:all)

                    render :back => '/app'

                  end

                   

                  To sort, just change this to:

                   

                  def index

                      @productcatalogs = ProductCatalog.find_all :order => :name

                      render :back => '/app'

                  end

                   

                  (Note that I've taken the liberty of simplifying by using find_all instead of find, and removing unnecessary parenthesis.)

                   

                  Couldn't be simpler.

                   

                  It sounds like maybe you need to review the basics of MVC architecture. It's much easier to understand Rhodes if you have some Ruby on Rails experience. Otherwise I agree the documentation is likely to leave you stumped. You really need to have a good understanding of how the Model, View, and Controller relate.

                   

                  You should endeavor to use as little Ruby code as possible in your views (.erb files). You shouldn't be using anything more than references to variables or functions. If you need something more complicated, put it in the controller and/or model. You can and should add your own controller and model methods. The boilerplate that Rhodes generates is just to get your started. It is useful to add little "helper" methods to the model (productcatalog.rb), for example:

                   

                  def find_all_order_by_name

                    find_all :order => :Name

                  end

                   

                  (Note that since we are in the model, it isn't necessary to explicitly mention the ProductCatalog class.)

                   

                  Then in the controller:

                   

                  def index

                    @productcatalogs = ProductCatalog.find_all_order_by_name

                    render :back => '/app'

                  end

                   

                  It's also useful to write little methods in the model to retrieve records by using some key:

                   

                  def find_by_name(name)

                      find_all :conditions => {:name => name} )

                  end

                   

                  So, in your controller if you needed all the records for a particular name:

                   

                    ProductCatalog.find_by_name 'widget'


                    • Re: Ordering data in RhoStudio

                      Thanks Jon, that worked great.

                       

                      I agree I need to continue reading documentation and trying to apply the lessons.  Could you recommend a document(s) or video that describes the files that get generated in RhoStudio when you create a project and a model and the function of each and how each one relates to MVC?  I'm new to this environment and I've got a lot to learn I know.

                       

                      Thanks again for your help.

                       

                      Gary