Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2009
Member
 
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.

Reply With Quote
  #2  
Old 03-02-2009
Member
 
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.
Reply With Quote
  #3  
Old 03-02-2009
Member
 
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]
Reply With Quote
  #4  
Old 03-02-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Fetching data from a database and display it in a view - Ruby on Rails"
Thread Thread Starter Forum Replies Last Post
CakePHP vs. Ruby on Rails pele1200 Polls & Voting 9 03-08-2012 05:26 PM
Information about Ruby on Rails Inigo Software Development 4 04-01-2011 06:36 AM
J2EE vs Ruby on rails KAMAL60 Software Development 7 28-07-2010 10:31 AM
Ruby On Rails vs. ASP.Net FlayoFish Software Development 2 17-04-2009 03:18 PM
where to use Ruby on Rails! machok Software Development 3 11-02-2009 11:15 PM


All times are GMT +5.5. The time now is 05:59 AM.