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.
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:
without further query.
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]
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