Rails 4.2 ActiveAdmin get admins email addr

2019-09-02 18:55发布

问题:

This query always returns [nil]

AdminUser.all.collect{|a|a.email}

However these two queries just work fine.

AdminUser.all
AdminUser.all.collect{|a| a.current_sign_in_ip}

Why can't i get the email addresses?

Update

The email field itself is not nil .It is present in the AdminUser.all call and also this call retuns the correct email.

>>au=AdminUser.find(1)
>>au[:email] 
=>my@email.addr

Solution to the original problem

Through another question/problem i figured out i added attr_accessor on my :email field in my model/admin_user.rb i removed it and now also the AdminUser.all.collect{|a|a.email}

query works.

回答1:

You have a typo in first query:

AdminUsers.all.collect{|a|a.email} # should be AdminUser

The better way is to use pluck for such queries:

AdminUser.pluck(:email)