Rails 4: Joins vs Includes: Why different results with nested association? -
Rails 4: Joins vs Includes: Why different results with nested association? -
in rails 4 app, have 2 models:
merchant has_many :offering_specials
offeringspecial belongs_to :merchant
i want retrieve merchants , special offerings open (with status_code: "op")
i tried this:
@merchants = merchant.joins(:offering_specials).where(offering_specials: { status_code: "op" })
this query:
merchant load (0.4ms) select `merchants`.* `merchants` inner bring together `offering_specials` on `offering_specials`.`merchant_id` = `merchants`.`id` `offering_specials`.`status_code` = 'op'
but retrieved offering specials, both open ("op") , pending ("pn").
however, using includes worked:
@merchants = merchant.joins(:offering_specials).where(offering_specials: { status_code: "op" })
this retrieved open offering specials. @ much slower query:
sql (19.9ms) select `merchants`.`id` t0_r0, `merchants`.`name` t0_r1, `merchants`.`slug` t0_r2, `merchants`.`url` t0_r3, `merchants`.`summary` t0_r4, `merchants`.`description` t0_r5, `merchants`.`active_for_display` t0_r6, `merchants`.`active_for_offerings_by_merchant` t0_r7, `merchants`.`active_for_offerings_by_legatocard` t0_r8, `merchants`.`credit_limit` t0_r9, `merchants`.`search_location_code` t0_r10, `merchants`.`image_file_name` t0_r11, `merchants`.`image_file_size` t0_r12, `merchants`.`image_content_type` t0_r13, `merchants`.`image_updated_at` t0_r14, `merchants`.`logo_file_name` t0_r15, `merchants`.`logo_file_size` t0_r16, `merchants`.`logo_content_type` t0_r17, `merchants`.`logo_updated_at` t0_r18, `merchants`.`created_at` t0_r19, `merchants`.`updated_at` t0_r20, `offering_specials`.`id` t1_r0, `offering_specials`.`special_number` t1_r1, `offering_specials`.`merchant_id` t1_r2, `offering_specials`.`merchant_user_id` t1_r3, `offering_specials`.`nonprofit_percentage` t1_r4, `offering_specials`.`discount_percentage` t1_r5, `offering_specials`.`start_at` t1_r6, `offering_specials`.`end_at` t1_r7, `offering_specials`.`closed_at` t1_r8, `offering_specials`.`max_dollar_amount_for_offering` t1_r9, `offering_specials`.`max_dollar_amount_per_buyer` t1_r10, `offering_specials`.`status_code` t1_r11, `offering_specials`.`created_at` t1_r12, `offering_specials`.`updated_at` t1_r13 `merchants` left outer bring together `offering_specials` on `offering_specials`.`merchant_id` = `merchants`.`id` `offering_specials`.`status_code` = 'op'
how can acquire query work joins, instead of includes?
queries of sort not homecoming associated records. you're requesting list of merchants, , that's get. when subsequently request associated offeringspecials of 1 of merchants, new query executed (which should see in logs), , of them, because did not specify otherwise. code in question not include place this, must doing somewhere, in order offeringspecials.
using includes
asks eager-load association, means subject restrictions of query, why you're seeing work when way. it's slower because it's fetching records now, instead of doing separately later.
if want refactor using .joins
, need add together conditional line fetch .offering_specials
of merchant:
@merchants.each |m| m.offering_specials.where(:status_code => 'op') end
however, should read on why eager loading exists before doing - either getting improve performance doing 1 slower query vs. many fast ones, or if number of merchant records involve passes threshold (which may or may not happen, depending on nature of app).
ruby-on-rails
Comments
Post a Comment