ActiveRecord Sum Max Joins 结果数组中有多个 nil id
ActiveRecord Sum Max Joins results in array with multiple nil ids
与Rails activerecord : sum, max and joins
相同的问题
除了得到奇怪的结果。
class CustomResumeline < ActiveRecord::Base
has_many :resumeline_words
end
class ResumelineWord < ActiveRecord::Base
belongs_to :custom_resumeline
end
CustomResumeline.joins(:resumeline_words).where(resumeline_words: {name: ["time", "data"]}).select('sum(resumeline_words.weight) as nb_votes').group('resumeline_words.custom_resumeline_id').order('nb_votes ASC')
结果
CustomResumeline Load (0.8ms) SELECT sum(resumeline_words.weight) as nb_votes FROM "custom_resumelines" INNER JOIN "resumeline_words" ON "resumeline_words"."custom_resumeline_id" = "custom_resumelines"."id" WHERE "resumeline_words"."name" IN ('time', 'data') GROUP BY resumeline_words.custom_resumeline_id ORDER BY nb_votes ASC
=> #<ActiveRecord::Relation [#<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, ...]>
我的问题是,为什么我会得到一个包含一堆 nil id CustomResumelines 的数组?
谢谢
根据您的查询,您正在使用 #select
到
result = CustomResumeline.joins(:resumeline_words)...
result
returns 一个 relation 对象,其中 id
将由 Rails 添加,因为您使用 #select
,虽然你还没有选择它。你现在可以做
result. map { |rec| rec.nb_votes }
# will give array of `nb_votes` values.
Selecting Specific Fields 指南中的一些提示:
Client.select("viewable_by, locked")
- to select only viewable_by and locked columns. Be careful because this also means you're initializing a model object with only the fields that you've selected. If you attempt to access a field that is not in the initialized record you'll receive: ActiveModel::MissingAttributeError: missing attribute: <attribute>
. Where <attribute>
is the attribute you asked for. The id
method will not raise the ActiveRecord::MissingAttributeError
, so just be careful when working with associations because they need the id method to function properly.
所以 id
方法不会引发 ActiveRecord::MissingAttributeError
,.. - 为什么? As #select
默认情况下将它添加到它创建的每个模型对象中,其值为 id
as nil
.
与Rails activerecord : sum, max and joins
相同的问题除了得到奇怪的结果。
class CustomResumeline < ActiveRecord::Base
has_many :resumeline_words
end
class ResumelineWord < ActiveRecord::Base
belongs_to :custom_resumeline
end
CustomResumeline.joins(:resumeline_words).where(resumeline_words: {name: ["time", "data"]}).select('sum(resumeline_words.weight) as nb_votes').group('resumeline_words.custom_resumeline_id').order('nb_votes ASC')
结果
CustomResumeline Load (0.8ms) SELECT sum(resumeline_words.weight) as nb_votes FROM "custom_resumelines" INNER JOIN "resumeline_words" ON "resumeline_words"."custom_resumeline_id" = "custom_resumelines"."id" WHERE "resumeline_words"."name" IN ('time', 'data') GROUP BY resumeline_words.custom_resumeline_id ORDER BY nb_votes ASC
=> #<ActiveRecord::Relation [#<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, #<CustomResumeline id: nil>, ...]>
我的问题是,为什么我会得到一个包含一堆 nil id CustomResumelines 的数组?
谢谢
根据您的查询,您正在使用 #select
到
result = CustomResumeline.joins(:resumeline_words)...
result
returns 一个 relation 对象,其中 id
将由 Rails 添加,因为您使用 #select
,虽然你还没有选择它。你现在可以做
result. map { |rec| rec.nb_votes }
# will give array of `nb_votes` values.
Selecting Specific Fields 指南中的一些提示:
Client.select("viewable_by, locked")
- to select only viewable_by and locked columns. Be careful because this also means you're initializing a model object with only the fields that you've selected. If you attempt to access a field that is not in the initialized record you'll receive:ActiveModel::MissingAttributeError: missing attribute: <attribute>
. Where<attribute>
is the attribute you asked for. Theid
method will not raise theActiveRecord::MissingAttributeError
, so just be careful when working with associations because they need the id method to function properly.
所以 id
方法不会引发 ActiveRecord::MissingAttributeError
,.. - 为什么? As #select
默认情况下将它添加到它创建的每个模型对象中,其值为 id
as nil
.