Ruby on Rails Tuesday, September 16, 2014

On 16 September 2014 18:22, frocco <farocco@gmail.com> wrote:
> Hello,
>
> I have a products table that is related to category.
>
> category has many products.
>
> How do I query the products to also include the category?
>
> Once I do this, how to I show the category data in my template?

There is usually no need to ask for the products to include the
category. If, for example, you say
@products = Product.where some_condition

then in your template (or in the controller) you can use code such as
@products.first.category even though you have not specifically used
.includes in the query. Rails will fetch the category later if it is
needed.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvX415zXL%2B%3DFfAStdH0zvxEjCA3YMh6jPW7zyJtZ9esDg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment