Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

Rails replace simple ActiveRecord relation in case of big amount of data

In my app controller looks pretty simple:

class ProductsController
  before_action :set_company

  def index
    @products = company.products.includes(:shipment)
  end

  private

  def set_company
    @company = Company.find(params[:company_id])
  end
end

But what I worry about is the @product inside Index action was properly declared? What if I'll have millions of products? is there more efficient solution?

Model relations below:

class Products < ApplicationRecord
  belongs_to :company
  has_many :shipment
end

class Company < ApplicationRecord
  has_many :products
end

class Shipment < ApplicationRecord
  belongs_to :products
end
question from:https://stackoverflow.com/questions/65928032/rails-replace-simple-activerecord-relation-in-case-of-big-amount-of-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is definitely a problem if you have million of records because your query is going to be too big, in this case you can add pagination in any flavor or use any other strategy that reduce the number of records queried each time.

To do pagination in Rails you can use https://github.com/kaminari/kaminari but this is not the only strategy available to do this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...