csakon
csakon

Reputation: 611

Create an Array from a Table Column

I have a database table that has a column that I need as an array.

I've gathered the records I need with:

app.rb

@liquor = Venue.find(@venue.id).liquors_venues.where(:venue_id => @venue.id)

and I can view the data I want with:

view.erb

<% @liquor.each do |liquor| %> <%= liquor.liquor_id %><% end %>

This only gives me the data I want by running the loop. I really need @liquor.liquor_id, but as liquor_id is in a subarray, it's inaccessible. I need an array so I can run this code to determine whether a checkbox is checked or not:

view.erb

<%= @liquor.include?(vodka.id) ? "checked" : "" %>

Upvotes: 0

Views: 100

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

You can put it into an array...

all_liquor_ids = @liquor.map { |liquor| liquor.liquor_id }

<%= all_liquor_ids.include?("vodka.id") ? "checked" : "" %>

Hope this helps.

Upvotes: 1

Related Questions