2014-07-21 2 views
0

배열 내부에서 열을 정렬하려고하지만 순서가 지정되지 않은 것은 ASC없이 주문하지 않고 값을 표시하고 있습니다. 여기 보기의 배열 배열 문제

내 테이블 : 여기
|products| 
    |id| |money| |client_id| 
    1  1000  1 
    2  2000  4 
    3  2000  7 
    4  2000  5 
    5  2000  6 
    6  2000  3 
    7  2000  2 

|customers| 
    |id| |name| |lastname1| |lastname2| 
    1 Lionel  Messi  Bueno 
    2 Cristiano Ronaldo  Tiger 
    3 Cecs  Fabregas  Midtlon 
    4 Andres  Iniesta  Blanco 
    5 Neymar  Dos Santos Junior 

여기 내 컨트롤러

class ProductController < ApplicationController 
    def index 
    @products= Product.all 
    end 
end 

입니다 모델 여기

class Client < ActiveRecord::Base 
    has_many :products 

    def str_name 
    return lastname1.to_s+" "+lastname2.to_s+" "+name.to_s 
    end 
end 

class Product < ActiveRecord::Base 
    belongs_to :client 
end 

것은 내 생각이다

<table> 
    <tr> 
    <td>Money</td> 
    <td>Client</td>  
    </tr> 

    <% @products.each do |p| %> 
    <tr> 
    <td><%= p.money %></td> 
    <td><%= p.str_name %></td> 
    <% end %> 
    </tr> 
</table> 

나는 시도했지만 lastname1 오름차순으로 주문되지 않습니다

SELECT * FROM `clients` WHERE (`clients`.`id` = 1) 
SELECT * FROM `clients` WHERE (`clients`.`id` = 2) 
SELECT * FROM `clients` WHERE (`clients`.`id` = 3) 
SELECT * FROM `clients` WHERE (`clients`.`id` = 4) 

그리고 다음과 같이해야한다 :

<% @products.each do |p| %> 
    <tr> 
    <td><%= p.money %></td> 
    <td><% @a= p.client(:order=>"lastname1 ASC")%> <%= @a.str_name %></td> 
    <% end %> 

로그는

SELECT * FROM `clients` order by last_name1 ASC 

제발 누군가가 나를 도울 수 있습니까? 이것이 당신의 질문에 대한 대답,하지만 두 번째 td에서 ERB 태그가 페이지에 아무것도를 인쇄하지 않은 경우

+0

보기에서'<% @ a = p.client (: order => "lastname1 ASC") %>'하지 마십시오. 제어기에서 그것을 수행하고,보기에서'@ a'에 액세스하십시오. –

+0

@ 얇은 사람 어떻게 정답이 될 수 있겠습니까? 도와 줄수있으세요? –

답변

1
<% @a= p.client(:order=>"lastname1 ASC")%> <%= @a.str_name %></td> 

위의 줄은 기술적으로 아무것도 아닙니다. 당신이 이미 루프에 있고 현재 제품의 클라이언트를 물어볼 때, 몇 개가 돌아갈 것입니까? 하나만 맞죠? 맞습니다. 반대쪽에 belongs_to가 있다면 (나는 당신이하기를 희망합니다.이 대답은 그것에 의존합니다). 따라서 실제로 반복 할 때마다 하나의 요소 집합을 "정렬"하고있는 것입니다. 그래서 Select가 "order by"를 무시합니다. 그래도해야 할 일은 각 고객의 "lastname1"에 의해 주문 된 모든 제품의 목록을 얻는 것입니다. 이 작업은 사용자가하려는 것처럼 컨트롤러에서 수행하면 안됩니다. 컨트롤러에서 :

#In Rails 4 
@products = Product.joins(:client).order('clients.lastname1 ASC') 

#Other version (be careful where to use plurals and where not to) 
@products = Product.find(:all, :joins => :client, :order => 'clients.lastname1 ASC') 

그리고보기에 : 당신의 코드에서

<% @products.each do |p| %> 
    <tr> 
    <td><%= p.money %></td> 
    <td><%= p.client.str_name %></td> 
    </tr> 
<% end %> 
+0

고마워, 나는 그것에 따라 코드를 테스트하고 일했다, 아저씨 감사합니다, 당신은 영리합니다. –

0
<% @products.each do |p| %> 
    <tr> 
    <td><%= p.money %></td> 
    <td><% @a= p.client(:order=>"lastname1 ASC")%> <%= @a.str_name %></td> 
    <% end %> 

확실하지. 구문을 확인하십시오.

실제로 @a을 이와 비슷하게 설정하려는 경우 컨트롤러에서 수행하는 것이 가장 좋습니다.

그건 내가 알아 차린 것입니다. 코드를 계속 살펴보고 원하는 결과를 얻지 못하는 이유를 찾을 수 있는지 확인합니다.

+0

어떻게 올바른 해결책이 될 수 있습니까? –

0

, 각 제품은 하나 개의 클라이언트를 가지고 같은,하지만 난 당신이 코드를 게시하지 않았기 때문에 확신 할 수 없어 보인다 귀하의 제품 모델. 이 경우 p.client은 하나의 클라이언트 만 반환하므로 결과를 정렬 할 이유가 없습니다. ActiveRecord는 똑똑해서 그 단계를 건너 뛸 수 있습니다. 이것은 또한 당신의 로그에있는 쿼리를 설명 할 수 있습니다.

클라이언트와 제품간에 다 대다 관계를 원할 경우 조인 테이블을 만들어야합니다.

rails generate model ClientProduct client_id:integer product_id:integer 

귀하의 결과 관계

는 것

클라이언트 :

class Client < ActiveRecord::Base 
    has_many :client_products 
    has_many :products, :through => :client_products 
    ... 
end 

제품 :

class Product < ActiveRecord::Base 
    has_many :client_products 
    has_many :clients, :through => :client_products 
    ... 
end 

ClientProduct :

class ClientProduct < ActiveRecord::Base 
    belongs_to :client 
    belongs_to :product 
    ... 
end