gpt4 book ai didi

mysql - Rails 连接查询不像 SQL 连接查询那样响应

转载 作者:行者123 更新时间:2023-11-29 07:40:37 24 4
gpt4 key购买 nike

我有两个表,package 和 time_slot,我想加入并返回一些值

在我的package.rb中

class Package < ActiveRecord::Base
attr_accessible :company_id, :time_slot_id, :location_id, :service_id, :day_id, :resource_quantity, :package_category_id, :time_slot, :is_booked
belongs_to :time_slot
end

在我的 time_slot.rb

class TimeSlot < ActiveRecord::Base
attr_accessible :start_time, :end_time
has_many :package
end

在我的 sql 查询中,当我运行此查询时,它运行良好

SELECT packages.id, time_slots.start_time, time_slots.end_time FROM packages INNER JOIN time_slots ON packages.time_slot_id = time_slots.id;

这就是结果 enter image description here

但是当我在 Rails c 中运行相同的查询时

Package.joins("LEFT JOIN time_slots ON packages.time_slot_id = time_slots.id").select("packages.id, time_slots.start_time, time_slots.end_time")

它只返回这个

 #<ActiveRecord::Relation [#<Package id: 1>, #<Package id: 2>, #<Package id: 3>]>

有人知道为什么吗?任何帮助表示赞赏

最佳答案

它不会“返回”那个。这就是它返回的内容的 to_s 表示形式。

Package 模型没有 start_time 或 end_time 属性,因此控制台在检查关系时不会显示所选值。

但是每个对象在调用时都会返回这些值。再次运行查询并尝试添加:

.first.start_time

或者

.map(&:attributes)

关于mysql - Rails 连接查询不像 SQL 连接查询那样响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29190819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com