Passing record query to session
Can som开发者_运维问答eone please tell me how to access the array that would be in @brand_found (below).
I have tried @brand_found.id, @brand_found[:id], @brand_found_id... nothing seems to work.
@brand_found = Brand.where("name = ?", params[:brand])
I would like to pass the id found to the session.
Thanks!
As you said, it's an Array, and an Array doesn't have any id.
You should iterate its elements
@brand_found.each_with_index do |element, index|
session[:"brand_{index}"] = element.id
end
It will store the ids in session[:brand_0]
, session[:brand_1]
etc...
Otherwise, take the first element and store it's id directly, just append .first
@brand_found = Brand.where("name = ?", params[:brand]).first
@brand_found.id #=> brand id
because where
is returning an array of objects.
精彩评论