Results 1 to 4 of 4

Thread: Fetching data from a database and display it in a view - Ruby on Rails

  1. #1
    Join Date
    Jan 2009
    Posts
    96

    Fetching data from a database and display it in a view - Ruby on Rails

    Hello!

    I have a problem with the classic example of Web Development with Rails, for those who do not know the database table there is a product, I wanted to add a table to distinguish their categories.

    In the code model of product product.rb I added the link to the categories

    Code:
     class Product <ActiveRecord:: Base 
       [...] 
        belongs_to: categories
    in that category category.rb I did the same.

    Code:
     class Category <ActiveRecord:: Base 
        has_many: products 
     end
    Now I want to retrieve the category to which the book as well, again in the product model product.rb, I have defined

    Code:
    Final find_category 
          @ category = Product.find (: all, 
            : joins => "as p inner join categories as c on c.id = p.category_id" 
            : select => "c.category") 
     end
    and amended accordingly view with the call to the function just defined the category to see the next book title

    Code:
    <h1> <% = I18n.t 'main.title'%> </ h1> 
      <% For product in @ products -%> 
          <div class="entry"> 
              <% = Image_tag (product.image_url)%> 
              <h3> <% = h product.title%> / <% = h product.find_category%> </ h3> 
              <% = Product.description%> 
              <span class="price"> <% = number_to_currency (product.price)%> </ span> 
                <% Form_remote_tag: url => (: action =>: add_to_cart,: id => product) do%> 
                  <% = Submit_tag I18n.t ( 'main.button.add')%> 
                <% End%> 
          </ div> 
      <% End%>
    Unfortunately the result is not what I expected, so as you can see from the following image.


    It seems to me an array is returned (?), Or three elements, which correspond to the number of books belonging to that category, actually running the query from the SQL prompt.

    Among other things I would also create a menu on the left with the groups which link to their books, but I don't understand how to solve this problem.

  2. #2
    Join Date
    Dec 2008
    Posts
    69

    Re: Fetching data from a database and display it in a view - Ruby on Rails

    Hello,
    with one to many association with belogs_to you need to put the singular

    Code:
     class Product <ActiveRecord:: Base 
       [...] 
        belongs_to: category
    then if you have an instance of Product and want to know your class well enough that you:

    Code:
     @ product.category
    without further query.

  3. #3
    Join Date
    Jan 2009
    Posts
    96

    Re: Fetching data from a database and display it in a view - Ruby on Rails

    Hello and thanks for your answer!
    I changed the association and also changed the view by changing the tag

    Code:
    <% = h product.category%>
    doing so displays something like # <Category:0x479b978>, is a question of casting?
    If in doubt, I also commented on the role that I defined in the model of the product.
    The log seems ok

    Code:
    Processing StoreController # index (for 127.0.0.1 at 2009-02-02 22:01:29) [GET] 
        Session ID: 15c18f2c19c685bbb3987578c62a64a6 
         [4, 35; 1mProduct Load (0.0ms) [0m [0mSELECT * FROM `products` ORDER BY title [0m 
      Rendering template within layouts / store 
      Rendering store / index 
         [4, 36; 1mProduct Columns (0.0ms) [0m [0; 1mSHOW FIELDS FROM `products` [0m 
         [4, 35; 1mCategory Columns (0.0ms) [0m [0mSHOW FIELDS FROM `categories` [0m 
         [4, 36; 1mCategory Load (0.0ms) [0m [0; 1mSELECT * FROM `categories` WHERE ( `categories`. `Id` = 1) [0m 
         [4, 35; 1mCACHE (0.0ms) [0m [0mSELECT * FROM `categories` WHERE ( `categories`. `Id` = 1) [0m 
         [4, 36; 1mCACHE (0.0ms) [0m [0; 1mSELECT * FROM `categories` WHERE ( `categories`. `Id` = 1) [0m 
      Rendered store / _cart (0.0ms) 
      Completed in 47ms (View: 47 DB: 0) | 200 OK [http://localhost/store]

  4. #4
    Join Date
    Dec 2008
    Posts
    112

    Re: Fetching data from a database and display it in a view - Ruby on Rails

    product.category returns an object of type class. # <Category:0x479b978> Is the default representation of an object that indicates the class name and the id (not to ActiveRecord).
    What you do is you want to access an attribute of a class project, for example, that we both description, then you must do this:

    Code:
     @ product.category.description

Similar Threads

  1. CakePHP vs. Ruby on Rails
    By pele1200 in forum Polls & Voting
    Replies: 9
    Last Post: 03-08-2012, 05:26 PM
  2. Information about Ruby on Rails
    By Inigo in forum Software Development
    Replies: 4
    Last Post: 04-01-2011, 06:36 AM
  3. J2EE vs Ruby on rails
    By KAMAL60 in forum Software Development
    Replies: 7
    Last Post: 28-07-2010, 10:31 AM
  4. Ruby On Rails vs. ASP.Net
    By FlayoFish in forum Software Development
    Replies: 2
    Last Post: 17-04-2009, 03:18 PM
  5. where to use Ruby on Rails!
    By machok in forum Software Development
    Replies: 3
    Last Post: 11-02-2009, 11:15 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,560,894.44810 seconds with 17 queries