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.
Bookmarks