when you write
@inventories = Inventory.find :first, :conditions => {:siteId => params[:siteId]}
Above line give you a single (first) object of the Inventory
depending on the default order which matched the given condition so you can use @inventories.siteId
. However when no row matches the given condition it will return nil and in such case if you try to use @inventories.siteId
it will throws an error undefined method
siteId' for nil`
But when you write
@inventories = Inventory.find :all, :conditions => {:siteId => params[:siteId]}
Above line gives you an array of the objects of Inventory
no matters if your query returns the 1 or more than 1 objects i.e. row and blank array i.e []
if now rows satisfies the condition. So, when you try to use @inventories.siteId
, you are actually applying siteId on Array and not on the object of Inventory
and it will throws an error undefined method
siteId' for #Array`. However following will work properly
@inventories.each{|p| puts "#{p.siteId}"}