【问题标题】:how to make this method more efficient Rails 3?如何使这种方法更高效 Rails 3?
【发布时间】:2015-01-02 11:06:29
【问题描述】:

我想让我的方法更有效。我的方法需要很长时间才能做出反应。所以请编辑我的代码以获得更有效的响应。如果您使这种方法更有效,我非常感谢您。并且您是 ruby​​ on rails 的大师,那么如果您可以使用 joins 制作此方法,那么对我来说这是一个完整的方法。 谢谢

 def all_shows_with_videos
    @arr = []
    tvs = Tv.all
    tvs.each do |tv|
      tv_tmp = {:name => tv.name, :id => tv.id}
      tv_tmp[:videos] = tv.videos
      tv_tmp[:seasons] = []
      season_tmp = {}
      tv.seasons.each do |season|
        season_tmp = {:name => season.name, :id => season.id}
        season_tmp[:videos] = season.videos
        season_tmp[:episodes] = []
        season.episodes.each do |episode|
        season_tmp[:episodes] << {:name => episode.name, :id => episode.id} if episode.videos?
        end
        tv_tmp[:seasons].push(season_tmp) if !season_tmp[:videos].blank? or !season_tmp[:episodes].blank?
      end

      @arr.push(tv_tmp) if !tv_tmp[:videos].blank? or !tv_tmp[:seasons].blank?
    end

    @arr = Kaminari.paginate_array(@arr).page(params[:page]).per(5)
    respond_to do |format|

      format.json {render :json => @arr}
    end
  end

输出是

[
    {
        "name": "Iron Man",
        "id": 95,
        "videos": [
            {
                "id": 1,
                "name": "Trailer 1",
                "site": "Youtube.com",
                "link": "Google.com",
                "quality": null,
                "video_type": null,
                "videoable_id": 95,
                "videoable_type": "Tv",
                "created_at": "2014-05-26T07:05:39+05:00",
                "video_source": null,
                "video_source_cd": null
            }
        ],
        "seasons": []
    },
    {
        "name": "How I Met Your Mother",
        "id": 100,
        "videos": [
            {
                "id": 13,
                "name": "Trailer 1",
                "site": null,
                "link": "google.com",
                "quality": "1020",
                "video_type": "Trailer",
                "videoable_id": 100,
                "videoable_type": "Tv",
                "created_at": "2014-06-09T10:05:03+05:00",
                "video_source": null,
                "video_source_cd": null
            }
        ],
        "seasons": []
    },
    {
        "name": "my tv",
        "id": 124,
        "videos": [
            {
                "id": 59,
                "name": "Trailer 1",
                "site": null,
                "link": "google.com",
                "quality": "1020",
                "video_type": "Trailer",
                "videoable_id": 124,
                "videoable_type": "Tv",
                "created_at": "2014-06-20T06:59:32+05:00",
                "video_source": null,
                "video_source_cd": null
            }
        ],
        "seasons": []
    },
    {
        "name": "Game of Thrones",
        "id": 151,
        "videos": [
            {
                "id": 129,
                "name": "",
                "site": null,
                "link": null,
                "quality": null,
                "video_type": "Season",
                "videoable_id": 151,
                "videoable_type": "Tv",
                "created_at": "2014-09-02T11:13:40+05:00",
                "video_source": null,
                "video_source_cd": null
            },
            {
                "id": 130,
                "name": "",
                "site": null,
                "link": "",
                "quality": null,
                "video_type": null,
                "videoable_id": 151,
                "videoable_type": "Tv",
                "created_at": "2014-09-02T11:13:40+05:00",
                "video_source": null,
                "video_source_cd": null
            },
            {
                "id": 131,
                "name": "",
                "site": null,
                "link": "",
                "quality": null,
                "video_type": null,
                "videoable_id": 151,
                "videoable_type": "Tv",
                "created_at": "2014-09-02T11:13:40+05:00",
                "video_source": null,
                "video_source_cd": null
            }
        ],
        "seasons": []
    },
    {
        "name": "Under the Dome",
        "id": 160,
        "videos": [],
        "seasons": [
            {
                "name": "Season Specials",
                "id": 267,
                "videos": [],
                "episodes": [
                    {
                        "name": "Inside Chester's Mill",
                        "id": 1112
                    }
                ]
            }
        ]
    }
]

【问题讨论】:

  • 这个问题属于codereview.stackexchange.com
  • 这里的一个大问题是您正在为所有 Tv 实例填充此数据,但随后只使用其中的 5 个

标签: ruby-on-rails ruby json ruby-on-rails-3 rubyzip


【解决方案1】:
  • 第一步是将您的逻辑转移到模型中,这样您的控制器将变得更精简

在您的 Tv 模型中

def self.all_with_seasons_and_episodes
  tvs = includes(:videos, :seasons => [:videos, :episodes]).all
  # loads all tvs and all its videos and seasons, seasons videos and episodes
  tvs.map do |tv|
    {
      name:    tv.name,
      id:      tv.id,
      videos:  tv.videos,
      seasons: tv.map_seasons
    }
  end
end

private

def map_seasons
  seasons.map do |s|
    {
      id: s.id,
      name: s.name,
      videos: s.videos,
      episode: s.episodes.map {|e| {name: e.name, id: e.id} }
    }
  end
end

现在你可以在like中使用

def all_shows_with_videos
  arr = Tv.all_with_seasons_and_episodes
  @arr = Kaminari.paginate_array(arr).page(params[:page]).per(5)
  respond_to do |format|
    format.json {render :json => @arr}
  end
end

【讨论】:

  • 开始 GET "/tv/all_shows_with_videos?api_key=moviedb-registration" for 127.0.0.1 at 2015-01-02 18:43:47 +0500 SyntaxError (/www/projects/movie_db/app/ models/tv.rb:574: 语法错误,意外的keyword_end,期望输入结束):app/controllers/api/v1/tvs_controller.rb:1:in `'
  • NoMethodError (私有方法map_seasons' called for #&lt;Tv:0x00000008799840&gt;): app/models/tv.rb:128:in block in all_with_seasons_and_episodes' app/models/tv.rb:123:in map' app/models/tv.rb:123:in all_with_seasons_and_episodes' app/controllers/api/v1/tvs_controller.rb:454:in `all_shows_with_videos'
  • 方法map_seasons 中缺少do 我已编辑帖子
猜你喜欢
  • 1970-01-01
  • 2014-03-26
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 2012-09-18
  • 2012-06-15
相关资源
最近更新 更多