I am trying to create an array of emails from my user model, with a helper method to access them. I will then load this array into a drop down selector. I am new to rails, and the MVC framework is new to me also.
def create_user_selector_array
@user = User.select(:email).distinct
end
<%= f.select(:associate, @user.map { |value| [ value, value ] }) %>
<%= @user %>
What appears to be happing when I launch this site, is that my selector contains this:
#<User:>(Characters)
And when I pull just the array it prints this:
ActiveRecord::Relation::ActiveRecord_Relation_User:0x007fb28894d508>
I feel close, but I'm not entirely sure what I'm doing wrong here.
In Rails 3.2 and newer, you can use pluck
:
User.pluck(:email)
# => ["foo@example.com", "bar@example.com"]
Otherwise, your solution just needed a map
:
@user = User.select(:email).distinct.map(&:email)
Using select
just determines what fields get returned, however you still get an array of ActiveRecord objects, not strings like you were expecting.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.