ruby on rails - How to get the value of include with conditions? -


i have 3 table shop, item , item_type

the shop contains names of shops , item contains items of each shop , item_type contains different types of items along status such available or not available.

now want render

format.json { render json: {:shop => @shops.as_json(:include => :items)}} 

but based on condition, items item_type_id='1' , status of item_type_status='available'

try this:

@shops = shop.includes(:items).where("items.itemp_type = ?", 'accesories') format.json { render json: { :shop => @shops.as_json(:include => :items) } } 

edited:

one way create hash objects want render, , pass render method. so:

respond_to |format|   format.json  { render :json => {:shops => @shops,                                    :items => @items }} end 

if models aren't associated through active record, that's best solution.

if association exist, can pass :include argument render call, so:

respond_to |format|   format.json  { render :json => @shops.to_json(:include => [:items])} end 

note wouldn't have retrieve @items variable in section above if take approach, rails automatically load @shops variable.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -