Home Ruby: index wont return the result for second parameter if same parameter passed twice with second parameter value blank
Reply: 0

Ruby: index wont return the result for second parameter if same parameter passed twice with second parameter value blank

mike
1#
mike Published in 2017-11-14 17:40:06Z

I have following index method in controller

def index
  if params.key?(:item)
    @items = Item.get_items.where(item: params[:item])
  else
    @items = Item.get_items
end

Model:

Class Item < ApplicationRecord
  has_many :item_resources, dependent: :destroy
  has_many :resources, through: :item_resources

  def self.get_items
    includes(item_resources: {resource: :item_groups})
      .order('item_resources.area ASC')
  end
end

Views:

index.json.jbuilder

json.array! @items, partial: ‘items/item’, as: :item
_item.json.jbuilder

_item.json.jbuilder

json.something1 item.something1
json.item_layout do
  json.template_key item.layout
  json.merge! item.layout_config if item.layout_config.present?
end
json.something (item.item_details) do |item_detail|
  json.partial! ‘item_details/item_detail’, item_detail: item_detail
end

I am using postman for testing as follows

http://localhost:3000/items?item=some_item
http://localhost:3000/items?item=some_item&item=other_item

The above works fine

http://localhost:3000/items?item=some_item&item=

When i try to pass item twice with second parameter value blank it wont return the result for second parameter but returns result for first parameter.

It generates sql with IN ('some_item', '')

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319312 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO