byCoder
byCoder

Reputation: 9184

Ruby select options if statement

In my db i have 3 fields:

name, cost, active

How can i display in select (form) only that values, on which active field is true(1)? Now i have such code:

= f.select :shipping_id, [["Select", "0"]] + @shippings.map{ |c| [c.name, c.id]}, {:required => true}

How to map, only if active is true?

Note! i must done it only in view!

Upvotes: 0

Views: 303

Answers (2)

shweta
shweta

Reputation: 8169

in your controller

@shippings = Shipping.where(:active => 1)

Upvotes: 0

Yunwei.W
Yunwei.W

Reputation: 1599

Try:

@shippings.select{|s| s.active}.map{|c| [c.name,c.id]}

Upvotes: 1

Related Questions