1

In my Rails app, I have a store with products and users. Both of those have models and controllers. What I want to achieve is to show on a view template a product to a current_user based on the attribute from a model that he has. For an example if a user has "Female" attribute from user model, and then to show some products related to this attribute. How can I achieve this?

These are my product views where all products are showed:

<% @products.each do |product| %>
      <%= render "product_row", product: product, order_item: @order_item %>
    <% end %>

_product_row.html.erb

<h4><%= product.name %></small></h4>
  <div class="image">
     <%= image_tag product.image.url(:original), class: "img-responsive" %></div>

    <p>Some description.</p>

   <a href="/bag/<%= product.id %>", class="add-to-cart">Add to Package</a>
Marko I.
  • 542
  • 10
  • 38

2 Answers2

1

It sounds like you need to retrieve objects from your product class based on an attribute from the User class. Sounds like a basic service object or just a method on the user:

class User < ActiveRecord::Base
  def products_for_gender
    if gender == 'female'
      Product.where("do some logic here based on female")
    elsif gender == 'male'
      Product.where("do some logic here based on male")
    else
      #do some other logic just in case it's nil
    end
  end
end

then in your controller you do this

@products = current_user.products_for_gender

in your view you then render a list with those products. This prevents you from putting logic in your view, which is rarely a good idea.

Also there's more abstraction possible, the if statement is not the prettiest, but this will cover your issue i believe. Eventually you could look into using service objects for example, https://blog.engineyard.com/2014/keeping-your-rails-controllers-dry-with-services

Marthyn Olthof
  • 1,029
  • 11
  • 22
  • Thank you Marthyn! by "do some logic here based on female" what do you mean? I should place attribute of a product there or? – Marko I. Jun 19 '16 at 12:30
  • No what i mean is that i do not know what logic you are using, based on what attribute of Product do you want to filter? So if gender is female, get all products where some attribute is some value. Product.where(attribute: "value"), this is for you to decide, i do not know what attributes you have in your product table – Marthyn Olthof Jun 20 '16 at 08:41
  • I figured thank you! Would you please tell me, what if want to include several attributes from different columns? Let's say that I want to retrieve objects based on several user attributes? Not only for gender but for also other columns and then if all those match user attributes to display them? – Marko I. Jun 20 '16 at 12:14
  • 1
    It totally depends on the use case of course. But you can add more if statements of course. In a project I'm working on we have a lot of different attributes that make up the articles we load. For example if a current_user has preferred_category and prefered_tag you could query like this `Article.where(category: current_user.preferred_category, tag: current_user.prefered_tag)` I'd advise you to read http://guides.rubyonrails.org/active_record_querying.html on how to query active record resources. – Marthyn Olthof Jun 20 '16 at 14:46
  • No problem, glad to help :) Could you mark the answer as the correct answer? – Marthyn Olthof Jun 22 '16 at 12:51
0

How are you saving the users model. If you are using devise then you have current_user helper method available in your views and you can use that to get the curren_user.gender attribute and show the view based on this

Saad
  • 1,856
  • 1
  • 22
  • 28
  • Yes I'm using standard Devise configuration.. I understand that but how the code should look like in the views? – Marko I. Jun 18 '16 at 23:27
  • It is up to you to decide what kind of products a female can have, that is more of a business logic which you didn't mention in the question – Saad Jun 19 '16 at 03:15
  • I posted an answer below, but reading the following SO question/answer will also help you http://stackoverflow.com/questions/60658/rails-model-view-controller-and-helper-what-goes-where?rq=1 – Marthyn Olthof Jun 19 '16 at 11:36