2014-04-09 5 views
0

희망자는 도움을받을 수 있습니다. 다음 및 이전 버튼이 데이터베이스 내의 다음 레코드로 작업하고 있습니다. 그러나 각 프로젝트는 유형에 속하며 단추가 동일한 유형의 다음 레코드로 이동할 수 있어야합니다.다음으로, 그룹 내의 이전 기록 (다수 있음)

모델

Project.rb 
has_many :typelinks, :dependent => :destroy 
has_many :types, :through => :typelinks 

def self.get_previous_project(current_project)   
    Project.where("projects.id < ? ", current_project.id).order('id asc').last 
end 

def self.get_next_project(current_project) 
    Project.where("projects.id > ? ", current_project.id).order('id asc').first 
end 

Type.rb 
has_many :typelinks, :dependent =>:destroy 
has_many :projects, :through =>:typelinks 

typelink.rb 
belongs_to :project 
belongs_to :type 

은 위 프로젝트와 유형 간의 관계 설정이다. 아래는 프로젝트의보기 페이지입니다. 마지막 또는 첫 번째 레코드가 있으면 화살표 숨기기. 요약

에서

<% unless @project.id == Project.first.id %> 
    <span id="leftarrow" class="prev"><%= link_to image_tag('/assets/prev_notext.png') + "", Project.get_previous_project(@project) %></span> 
<% end %> 

<% unless @project.id == Project.last.id %> 
    <span id="rightarrow" class="next"><%= link_to image_tag('/assets/next_notext.png') + "", Project.get_next_project(@project) %></span> 
<% end %> 

이것은 요구되는 기능의 예이다 (그냥 순간하는 기능을 필요로하는 CSS 제거).

Project1의 Project2에 프로젝트 3 Project4

타입 1 타입 2

는 Project2에와 Project4는 타입 2에 속한다.

Project2의보기 페이지에서 다음 화살표를 클릭하면 Project4보기가 아닌 Project3으로 이동해야합니다.

누군가가 이것을 도와 줄 수 있기를 바랍니다. 오랫동안 유감스럽게 생각합니다.

답변

1

이 작동하지 않을 수 있습니다 : 대한 답변을

Project.joins(:types).where("projects.id < ? and types.id in (?)", current_project.id, current_project.types.map(&:id)).order('id asc').last 
+0

감사합니다, 그러나 그것은 단지 옆이 아닌 유형의 데이터베이스에서 다음 레코드로 것입니다. – Clay

+0

잠깐, 작동하지 않습니다. 다음을 클릭하십시오. 이전에 더 많은 테스트를 수행합니다. – Clay

+0

고마워요, 유일한 것은 해당 유형의 마지막 레코드에 도달하면 마지막 레코드로 이동하고 첫 번째 프로젝트와 동일합니다 – Clay