On Friday, 11 April 2014 07:13:25 UTC-4, Ruby-Forum.com User wrote:
>
> Ups i forget the visible condition 
>
> B.where(a_id: @As, is_visible: true) 
>
> if you have a massive amounts of Bs the Data Base server will hate 
> you,so a better solution is 
>
> @Bs=Array.new 
> @As.each do |a| 
>   @Bs<<B.where(a_id: a, is_visible: true) 
> end 
>
>
If you have "massive amounts" of B records, the DB server won't be the 
bottleneck - instantiating all those objects will be. Doing more SQL 
queries is usually the opposite of optimization.

I'd also recommend you check out the associations guide on 
guides.rubyonrails.org. It's possible to do things like:

  class Blog < ActiveRecord::Base
    has_many :visible_posts, -> { where(is_visible: true) }, class_name: 
'Post'
  end

  class Post < ActiveRecord::Base
    belongs_to :blog
  end

  @blogs = Blog.includes(:visible_posts).find_by_whatever(...)
  @too_many_posts = @blogs.map(&:posts)

--Matt Jones


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/bce19a8b-a18e-468d-9d7c-b60a0cf5e62c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to