0

I've in my app, posts as ideas, and these ideas belongs to an activity and a status. And I want to sort them by activity for one status so I did in my controller

@idees_en_place = Idee.where(statut_id= "2")
@activites = Activite.all

And in my view :

<% @activites.each do |activite| %>
  <div class="idee en-place col-lg-5" style="background:#<%= activite.color%>">
    <h2><%= activite.name %></h2>
    <p>
     <% @idees_en_place.where(activite_id = activite.id).limit(3).each do |idee| %>
     <div class="idee">
       <h6><%= link_to idee.title, idee %></h6>
      </div>
     <% end %>
  </p>
  </div>
<% end %>

But that doesn't work, in each part of an activity the ideas are not sorted. I think it's a little mistake but I don't know how to resolve this

2
  • 2
    should be: @idees_en_place = Idee.where(statut_id: 2) Commented May 19, 2015 at 8:30
  • Can you add how you mapped your models? Commented May 19, 2015 at 8:55

4 Answers 4

1
@idees_en_place = Idee.where(statut_id= "2")

There are two problems with this code.

First, id is a Integer type (unless you've defined it as String). Second, its a key value you pass to where clause, and you pass these either as

:status_id => 2 # old hashrocket syntax

or

status_id: 2 # new syntax

The same goes with this part

@idees_en_place.where(activite_id = activite.id)

it should be

@idees_en_place.where(activite_id: activite.id)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ! But now, no ideas are displayed
@MilletoSimon this is a question to you and associations between models in your app, not to me :) Most likely there is no Ideas with status_id of 2. Please accept the answer if it taught you something about quering db
0

In Controller

@idees_en_place = Idee.where(statut_id: 2)
@activites = Activite.all

In View

<% @activites.each do |activite| %>
  <div class="idee en-place col-lg-5" style="background:#<%= activite.color%>">
    <h2><%= activite.name %></h2>
    <p>
     <% @idees_en_place.where(activite_id: activite.id).limit(3).each do |idee| %>
     <div class="idee">
       <h6><%= link_to idee.title, idee %></h6>
      </div>
     <% end %>
  </p>
  </div>
<% end %>

1 Comment

One should never execute queries in views. On 100 activities there will be 100 queries against database executed.
0

I just wanna point out that you will run into an N+1 queries issue, to avoid this you should preload every thing, instead of doing queries in the views.

The controller:

#change if the association name is different
@activites = Activite.includes(:idees)

The view

<% @activites.each do |activite| %>
  <div class="idee en-place col-lg-5" style="background:#<%= activite.color%>">
    <h2><%= activite.name %></h2>
    <p>
     <% activitie.idees[0..2].each do |idee| %>
     <div class="idee">
       <h6><%= link_to idee.title, idee %></h6>
      </div>
     <% end %>
  </p>
  </div>
<% end %>

Notes:

I've used the [0..2] format because I wanted to avoid ActiveRecord from doing a new query, another method would be limiting the query using something like this

@activites = Activite.includes(:idees).merge(Idee.limit(3))

Then you won't need to use any limitation in the views, but I haven't tested this, don't have access on a rails machine right now.

Comments

0

I think that the following code will help you:

Since your Idee belong to activity and status that's why you have activity_id and status_id in your Idee table.

you may find out all the idee for a status by using:

Idee.where(:status_id => 2)

and you can sort Idee in Asc or desc order by using order

idees = Idee.join(:activity).where(:status_id => 2).order(:activity_id => :asc)
idees = Idee.join(:activity).where(:status_id => 2).order(:activity_id => :desc)

<% activity_id = -1%>
<@idees.each do |idee| %>
  <div class="idee en-place col-lg-5" style="background:#<%= idee.activite.color%>">
  <h2><%= idee.activity.name %></h2>
  <p>
    <% if activity_id != idee.activity_id  %>
      <% activity_id = idee.activity.id %>
      <% counter = 0 %>
    <% end %>
    <% if counter < 3 %>
      <% counter = counter + 1%>
      <div class="idee">
        <h6><%= link_to idee.title, idee %></h6>
      </div>
    <% end %>
  </p>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.