ruby on rails中的sql查询

时间:2021-02-06 23:35:47

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

我有以下查询选择*,在Zips表中显示的位置等于users表中的列。

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

这很好,但是如何从Zips表中选择另一列来打印它呢?

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

邮政编码。(:显示= > @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...

例如,users表有一个location col, zips表有一个display和zipcode列。我通过将用户的位置匹配到以zips显示的位置来找到正确的行,但是需要从以zips匹配的条目中提取zipcode…

1 个解决方案

#1


1  

Assuming your Zip table has the zipcode field:

假设您的Zip表有zipcode字段:

@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>

#1


1  

Assuming your Zip table has the zipcode field:

假设您的Zip表有zipcode字段:

@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>