0

我试图从“has_one:through”关联中检索一个字段。假设我有以下型号:Rails 4:ActiveRecord =>不能从数据库中“选择”has_one:through字段

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 

..我想用ActiveRecord的是翻出只是基于的人的名字查询,以预测(我知道它很愚蠢,但这只是一个练习);所以是这样的:

# 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) 

或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 

这就是我一直是这样的:

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

然而,这也导致空预测的colleciton对象

然而,这样做的结果正是我想要的,但这已经是该数据库查询了后:

result = Forecast.joins(:person).where("precipitation > ?", 1.5) 

result.each do |forecast| 

    puts forecast.person.first_name # => "John", "Bob", "Jim" 

end 

为什么我不能使用select从数据库中获取first_name,并且只将first_name从数据库中取出?我明显错过了一些东西。

+0

另外,据我所知,我可能会添加一个范围,但是从我的理解范围只是要给我的能力运行手动查询并为我提供语法糖,例如select(“person.first_name”),如下所示:file:/// Users/tomek/Library/Application%20Support/Dash/DocSets/Ruby_on_Rails_4/Ruby%20on%20Rails。文档集/内容/资源/文件/ api.rubyonrails.org /班/ ActiveRecord的/作用域/命名/ ClassMethods.html#方法-I-范围。 但我想弄清楚为什么我不能通过选择项目,但我可以在每个方法。 – 2014-10-11 15:17:53

回答

1

我不知道为什么你的解决方案可以工作。

这样来做:

Forecast.where("precipitation > ?", 1.5).joins(:person).pluck("person.first_name") 

它会返回名字的数组。

如果你真的需要使用select(以获得范围):

Forecast.where("precipitation > ?", 1.5).joins(:person).select("person.first_name") 
+0

你为什么这样说:“我不知道你的解决方案为什么能起作用。” ?? – 2014-10-11 22:14:27

+1

因为你应该指定你想要加入的表。您正试图从Forecast中选择person first_name,但您不在join子句中指定person表。 ActiveRecord应该引发异常,因为它会尝试在预测表中找到first_name列。 – chumakoff 2014-10-12 12:06:16

+0

好吧,但我有一个关于Forecast to Person的has_one子句,是不是有助于自动创建连接? – 2014-10-12 13:06:27