0

I have the following query that selects * where display in the Zips table is equal to a column in the users table.

This works fine, but how do I select ANOTHER column from the Zips table to print it out?

Zip.where(:display => @user.location)

So, for example the users table has a location col and the zips table has a display and zipcode column. I am finding the proper row by matching location from users to the display in zips but need to pull out zipcodes from the matching entry in zips...

1 Answer 1

1

Assuming your Zip table has the zipcode field:

@zips = Zip.where(:display => @user.location)
@zips.each do |zip|
  puts zip.zipcode
end

Or to put them in an array:

zipcodes = @zips.collect{ |zip| zip.zipcode }

In a view:

<h3>Zip Codes</h3>
<ul>
  <% @zips.each do |zip| %>
  <li><%= zip.zipcode %></li>
  <% end %>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.