Ruby on rails Rails 4:ActiveRecord=>无法从数据库中“选择”具有一个:直通字段
我试图通过关联从has_one:中检索一个字段。假设我有以下模型:Ruby on rails Rails 4:ActiveRecord=>无法从数据库中“选择”具有一个:直通字段,ruby-on-rails,select,activerecord,ruby-on-rails-4.1,has-one-through,Ruby On Rails,Select,Activerecord,Ruby On Rails 4.1,Has One Through,我试图通过关联从has_one:中检索一个字段。假设我有以下模型: class CreatePersons < ActiveRecord::Migration def change create_table :person do |t| t.string :first_name t.string :last_name t.date :age end end def change create_table :loc
class CreatePersons < ActiveRecord::Migration
def change
create_table :person do |t|
t.string :first_name
t.string :last_name
t.date :age
end
end
def change
create_table :location do |t|
t.decimal :longitude
t.decimal :latitude
t.string :city
t.string :state
t.references :person
end
end
def change
create_table :forecasts do |t|
t.timestamp :time
t.text :summary
t.decimal : precipitation
t.references :location
t.timestamps
end
end
end
class Location < ActiveRecord::Base
belongs_to :person
belongs_to :forecast
end
class Forecast < ActiveRecord::Base
belongs_to :locaiton
has_one :person, :through => :location
end
class Person < ActiveRecord::Base
end
或者用SQL术语
select first_name from forecast fc
inner join locaiton loc on loc.id = fc.location_id
inner join person on person.id = loc.person_id
where precipitation > 1.5
所以我试过这样的方法:
# i realize this is a bad query, i'm putting this here to make my question stronger
Forecast.where("percipitation > ?", 1.5).select(:first_name)
Forecast.joins(:person).where("percipitation > ?", 1.5).select(:person)
# Forecast.joins(:person).where("percipitation > ?", 1.5).select(:person).to_sql
# select 'person' from forecast fc
# inner join locaiton loc on loc.id = fc.location_id
# inner join person on person.id = loc.person_id
# where fc.percipitation > 1.5
这将返回Forecast对象的空实例
于是我尝试了这个:
Forecast.joins(:person).where("percipitation > ?", 1.5).select("person.first_name")
# Forecast.joins(:person).where("percipitation > ?", 1.5).select("person.first_name").to_sql
# select 'person.first_name' from forecast fc
# inner join locaiton loc on loc.id = fc.location_id
# inner join person on person.id = loc.person_id
# where fc.percipitation > 1.5
但是,这也会导致收集空的预测对象
但是,这样做的结果正是我想要的,但这已经是在查询数据库之后:
为什么我不能使用select从数据库中获取第一个\u名称,并仅从数据库中提取第一个\u名称?我显然遗漏了一些东西。我根本不知道你的解决方案为什么有效 这样做:
Forecast.where("precipitation > ?", 1.5).joins(:person).pluck("person.first_name")
它将返回名字数组
如果确实需要使用select来获取范围:
Forecast.where("precipitation > ?", 1.5).joins(:person).select("person.first_name")
此外,我了解我可能会添加一个范围,但据我所知,scope只是让我能够运行手动查询,并为我提供语法糖,如selectperson.first\u name,根据:file:///Users/tomek/Library/Application%20Support/Dash/DocSets/Ruby_on_Rails_4/Ruby%20on%20Rails.docset/Contents/Resources/Documents/api.rubyonrails.org/classes/ActiveRecord/Scoping/Named/ClassMethods.htmlmethod-i-scope。但我试图弄明白为什么我不能通过select进行项目,但我可以通过一个each方法进行项目。为什么你说:我根本不知道你的解决方案为什么有效??因为您应该指定要加入的表。您正试图从Forecast中选择person first_name,但未在join子句中指定person表。ActiveRecord应该引发一个异常,因为它将尝试在Forecast表中查找first_name列。好的,但是我有一个关于Forecast to Person的has_one子句,这是否有助于自动加入?据我所知,没有。我已经测试过这个。好的,我也测试过它,我很困惑,你是对的,它不会自动给你关系!
Forecast.where("precipitation > ?", 1.5).joins(:person).select("person.first_name")