activerecord - Rails 4.0.2 select & uniq -


i'm using activerecord query

campaigntype.includes(:campaign_description)        .select('distinct campaign_description_id, campaign_description.name')        .where(:campaign_id => campaign_id) 

which used work on rails 3. gives lot of depecation warnings , example:

currently, active record recognizes table in string, , knows join comments table query, rather loading comments in separate query. however, doing without writing full-blown sql parser inherently flawed. since don't want write sql parser, removing functionality. on, must explicitly tell active record when referencing table string:  post.includes(:comments).where("comments.title = 'foo'").references(:comments) 

i've tried different ways i'm not getting there. appreciated.

i cannot explain why got error mentioned in comments, have done same on end , works properly. however, there issue may have effect of solving problem anyways.

unfortunately, while technically allowed, if use both includes eager loading in combination select, select ignored (columns not limited, dump of everything). if eager loading significant requirement in situation, should able use preload method instead of includes. query more this:

campaign_type = campaigntype.preload(:campaign_description)                 .select("id, campaign_description_id, campaign_description.name")                 .joins(:campaign_description)                 .where(:campaign_id => campaign_id)                 .uniq 

this have effect of eager loading associated campaign_description model, while still selecting columns specified. long campaigntype model allows accessing non-column attributes, should have no trouble doing things campaign_type.name. note uniq @ end adds distinct modifier select clause.

regarding joins method: unfortunately, still need include method, otherwise rails not implicitly perform inner join, , reference campaign_description in query result in error.

also note i've included id field in select. important, required in order load , access associated campaign_description rows. if did not include it, receive empty collection if tried campaign_type.campaign_description (as example).

however, in case, if want return model distinct fields , won't need call associated objects, might not need eager loading @ all, , can safely ditch preload , id part. else remain same.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -