-1

First, the example I read in the docs shows to declare the associated model as singular, :address, but if I do I get the error Association named 'address' was not found on User; If I change it to plural :addresses, then the next problem I have is the association doesn't work in views undefined method `country' for ... Why am I declaring the association as plural and how can I make the association available in the view

User.rb:

class User < ActiveRecord::Base
searchkick  word_middle: ['full_name', 'description', 'interests'] 
has_many :addresses
scope :search_import, -> { includes(:addresses) }

search.html.erb:


 <% @users.each do |u| %>
  <li> 
    <%= link_to "#{u.first_name} #{u.middle_name} #{u.last_name}", page_path(name: u.name) %>

        <% @ua=u.addresses.where("current=?", true) %>
        <% if @ua.country=="US" %>
            <%= @ua.city %>, <%= @ua.state %> <%= ISO3166::Country.find_country_by_alpha2(@ua.country) %>
        <% else %>
            <%= @ua.city %>, <%= ISO3166::Country.find_country_by_alpha2(@ua.country) %>
        <% end %>
  </li>
 <% end %>
</ul>

Mices
  • 89
  • 3
  • 9
  • 1
    You can get answers to all your questions here - https://guides.rubyonrails.org/. Please consider reading. Your error has nothing to do with associations or searchkick. – Gautam Feb 05 '20 at 04:38
  • I don't see the usefulness of your answer because it's too general, not specific to my question, and doesn't help me the least bit – Mices Feb 05 '20 at 04:58
  • @Mices Gautam's `comment`, not answer, might appear rude to you but it is right. You are missing some very basics. You either need a basic guide as a person or you need to read through documentations. – ARK Feb 06 '20 at 11:19
  • 1
    @Mices we can help you with this single case, but it's clear that you really need to stick with basics, because your code doesn't satisfy "rails way". So, shortly, here is what you need: to fetch all addresses with #includes method, that use it to take addresses from each user. But it's better to take addresses to the separate instance and do not save each address for each users and than go through addresses collection with the loop, and fetch addresses from separate table for each user as association, so you will be able to call attribute from user itself instead from addresses. – TiSer Feb 08 '20 at 20:33
  • @Mices Also you could use helper methods or decorator patter for such expression: "#{u.first_name} #{u.middle_name} #{u.last_name}" And never define variables with `where` inside the views - do it in the models, define variable in controller and then use inside the views. Just take a look at the basic guides, screencasts, maybe some youtube lessons. Good luck. – TiSer Feb 08 '20 at 20:33

1 Answers1

0
  • In controller, do this: @user = User.includes(:addresses).where(your_query) to make the association readily available in view.

  • And yes has_many associations are bound to be plural: "User has_one :life" and "User has_many :passions"; does it make sense?

  • And finally your error: where returns an array because you queried: "bring me all records which fulfill this condition". find, on the other hand, will bring back 1 specific record as it expects a unique attribute or brings back first record that matches that attribute.

  • What you need to do:

    You should either do this (if you are dead-sure that you will get 1 such record or you need only one of that type):

    <% @ua=u.addresses.where("current=?", true).first %>

    OR

    If you need to go through all the resultant array then:

  <% @ua=u.addresses.where("current=?", true) %>
  <% @ua.each do |ua| %>
    # Your code for each ua instead of @ua.
  <% end %>

Happy Learning :)

ARK
  • 772
  • 7
  • 21
  • Wow Thanks That really was very enlightening I hadn't though about that I thought limit 1 and first were the same thing – Mices Feb 08 '20 at 17:32
  • @Mices if this answer solves your issue, please accept the answer and upvote it afterwards. – ARK Feb 08 '20 at 21:11