开发者

Passing record query to session

开发者 https://www.devze.com 2023-03-26 13:25 出处:网络
Can som开发者_运维问答eone please tell me how to access the array that would be in @brand_found (below).

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消