Rails4:如何仅在每个roop

时间:2015-10-04 06:53:35

标签: ruby-on-rails ruby ruby-on-rails-4

我想将数据显示如下;

Day1
09:00 Math
11:00 Science
Room classA (3rd floor)

Day2
10:00 English
13:00 Music
Room classB (1st floor)

样本文章表

school_id day start_time title
1         1   09:00      Math
1         2   10:00      English
1         1   11:00      Science
2         1   12:00      Physics
1         2   13:00      Music

样品室表

school_id day name    detail
1         1   classA  3rd floor
1         2   classB  1st floor
2         1   classC  2nd floor

我想我应该在Rails中实现SQL如下所示来显示房间数据。

SELECT name, detail FROM rooms WHERE articles.school_id = rooms.school_id AND articles.day = rooms.day

尽管我尝试了一些代码,但是如果你能在Rails中给我最好的方法,我将不胜感激。

查看代码

<div class="row">
  <% @articles.each do |a| %>

      <%= a.start_time %>&nbsp;<%= a.title %><br>
      <!-- I can't display rooms.name and rooms.detail in the X Day last line-->

  <% end %>
</div>

控制器代码

def show
  @articles = Article.where(school_id: (params[:id])).order(day: :asc)

型号代码

class School < ActiveRecord::Base
    has_many :articles


class Article < ActiveRecord::Base
    belongs_to :school


class Room < ActiveRecord::Base
    #(nothing)

模式

ActiveRecord::Schema.define(version: 20150999999999) do

  create_table "rooms", force: true do |t|
    t.integer  "school_id"
    t.integer  "day"
    t.string   "name"
    t.string   "detail"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "articles", force: true do |t|
    t.integer  "school_id"
    t.integer  "day"
    t.string   "start_time"
    t.string   "end_time"
    t.integer  "category"
    t.string   "title"
    t.string   "contents"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "schools", force: true do |t|
    t.string   "title"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

如果你能给我任何建议,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以从数据库中获取数据,然后将其分配给某个局部变量

articles = Article.where(school_id: (params[:id]))

然后您可以按天分组记录:

articles_by_day = articles.group_by(&:day)

此操作的结果将是hash,其中days为键,文章数组为值。

然后我们需要在一天内按start_time对文章进行实际排序:

articles_by_day.values.each{|x| x.sort_by!(&:start_time)}

然后获取按日排序的文章数据:

@articles = articles_by_day.keys.sort.map{|k| [k, articles_by_day[k]]}

@articles中的结果值将是一个包含2个值元组的数组:一天中的日期和文章。比如[[1,[#{struct Article day = 1,name =&#34; Science&#34;,...},...]],...]

然后您可以通过以下方式在模板中使用它:

<% @articles.each do |(day, day_articles)| %>
   Day <%= day %><br>
   <% day_articles.each do |a| %>
     <%= a.start_time %>&nbsp;<%= a.title %><br>
   <% end %>
 <% end %>

答案 1 :(得分:0)

您正在寻找SQL GROUP BY功能;这意味着你可以.group Articles day

#config/routes.rb
resources :schools do 
   resources :rooms, controller: :articles #-> url.com/schools/:school_id/rooms/:id
end

#app/controllers/rooms_controller.rb
class RoomsController < ApplicationController
   def index
      @school = School.find params[:school_id]
      @room   = @school.rooms.find params[:id]
      @articles = room.articles.group(:day).order(day: :asc)
   end
end

这将为您提供一系列日期,其中包含文章的散列:

#app/views/articles/index.html.erb
<% @articles.each do |i,articles| %>
   <%= i %>
   <% articles.each do |article| %>
      <%= article.start_time %><%= article.title %>
   <% end %>
   <%= "Room #{article.room_name} #{article.room_detail}" %>
<% end %>

(除非您将articlesrooms关联起来,否则不会有效:

我认为您的代码存在问题;特别是您在未将roomsschool链接到article的情况下分配#app/models/room.rb class Room < ActiveRecord::Base #columns id | school_id | name | created_at | updated_at belongs_to :school has_many :class_rooms has_many :articles, through: :class_rooms end #app/models/class_room.rb class ClassRoom < ActiveRecord::Base #Divides Article + Room for each school and day #columns id | day | room_id | article_id | start_time | end_time | detail | created_at | updated_at #NB room_id should associate to each school. #Detail moved to ClassRoom (denotes which class will be in room) belongs_to :article belongs_to :room end #app/models/article.rb class Article < ActiveRecord::Base #columns id | category | title | contents | created_at | updated_at has_many :class_rooms has_many :rooms, through: :class_rooms has_many :schools, through: :class_rooms end #app/models/school.rb class School < ActiveRecord::Base #columns id | name | address | etc | created_at | updated_at has_many :rooms has_many :class_rooms has_many :articles, through: :class_rooms end

#Schools
id name      address
1  School A  1 Avenue
2  School B  2 Avenue
3  School C  3 Avenue

#Articles
id category title    contents
1  x        English  English Literature
2  x        Math     Basic Arithmetic
3  x        Science  Science

#Rooms
id school_id name
1  1         1st floor
2  2         2nd floor
3  1         3rd floor

#ClassRooms
id  day  room_id  article_id  start_time  end_time  detail
1   1    1        1           12:00       13:00     ClassB
1   2    1        1           12:00       13:00     ClassA

此类示例数据可能包括:

@school   = School.find params[:id]
@rooms    = @school.rooms
@articles = @rooms.find(params[:room_id]).articles.group(:day)

这将使您能够调用以下内容:

day

为此,您必须在has_many :through关联中加入<% articles.each do |day, articles| %> <%= "Day #{day}" %> <% articles.each do |article| %> <%= "#{article.start_time} #{article.title}" %> <% end %> <%= "Room #{article.room.name} (#{article.room.detail})" %> <% end %> 和一些额外的参数。

它将为您提供所有相关数据:

var res = cmd.Parameters["@RESULT"];

if(res != null && res.Value != null && res.Value != DbNull.value){
val = Convert.ToDecimal(res.Value);
}
else
val = defaultvalue;