0

I have such code

@pre_articles = Article.find(:all, :conditions => { :ART_ID => @linkla.map(&:LA_ART_ID)})
@articles = Kaminari.paginate_array(@pre_articles).page(params[:page]).per(15)

It's selecting for me array of data, but it's huge, so i decided to add pagination. It select's 15 entries for view, but also for every page (in log) i see, how sql is selecting all array as in first @pre_articles. For more speed: how to select on every page for example 0-15, 15-30, 30-45 etc entries and send it for view? now it's selecting all data, but dislpaying as i need

Oh sorry, important!:

@linkla = LinkArt.where(:LA_ID => @la_typs.map(&:LAT_LA_ID), :LA_GA_ID => @genart.map(&:GA_ID)) 
@articles = Article.where(:ART_ID => @linkla.map(&:LA_ART_ID)).page(params[:page]).per(15)

So looks my query. As you see depending on @linkla results i select articles, and link la is selecting many as before... how to do that he select only for my page

byCoder
  • 3,462
  • 6
  • 28
  • 49

2 Answers2

0

What about using where clause instead of conditional find?

@articles = Article.where(:ART_ID: @linkla.map(&:LA_ART_ID)).page(params[:page]).per(15)

The SQL query generated will include a LIMIT clause to avoid loading unnecessary data.

ZelluX
  • 69,107
  • 19
  • 71
  • 104
0

Solution to the stated problem:

LinkType
  has_many :links
  # let us assume there is a column called name 

# I wasn't sure about this model from your description
GenArt
  has_many :links
  # let us assume there is a column called name 

Link
  belongs_to :article
  belongs_to :link_type  
  belongs_to :gen_art
  # columns: id, article_id, link_type_id, gen_art_id

Article
  has_many :links

Assuming you the params hash contains link_type_names and gen_art_names

Article.joins(:links => [:link_type, :gen_art]).where(
  :links => {
    :link_type => {:name => params[:link_type_names]},
    :link_type => {:name => params[:gen_art_names]}
  }
).page(7).per(50)  
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
  • Can you post your models and their relationships in your question? Also can you list the input parameters that you are working with to the controller action. – Harish Shetty Jun 22 '12 at 21:08