From what i have read using a scope in Rails is more efficient as your querying the model directly as opposed to an instance variable which goes via the controller to the model (thats how i see it?)
So i have this query in my controller
@animal_location = User.select(:town).map(&:town).uniq
["Cardiff", "Newport"]
and then this scope in my model
scope :uniq_towns, ->() {
select("town").group("town")
}
#<ActiveRecord::Relation [#<User id: nil, town: "Cardiff">, #<User id: nil, town: "Newport">]>
In my view to access the town value using @animal_location = User.select(:town).map(&:town).uniq I can access like
<% @animal_location.each do |loc| %>
<%= loc %>
<% end %>
or if i used the scope and went with @animal_location = User.uniq_towns, in my view i would use
<% @animal_location.each do |loc| %>
<%= loc.town %>
<% end %>
My first question is would my scope be quicker in this instance and secondly is my scope correct as i am getting User id: nil as part of the hash
Thanks