【问题标题】:ActiveRecord not retrieving all modelsActiveRecord 未检索所有模型
【发布时间】:2015-01-06 05:51:39
【问题描述】:

我有 2 个模型,汽车和人。每个人可以有很多辆车。我已经从seed.rb 加载了一些数据,但是当我在控制台中尝试Car.find_by(owner_id: 1) 时,我只看到一辆车。当我期待 2 辆车时,因为我将 2 辆车分配给了一个特定的人。为什么我只看到一辆车?

car.rb(型号)

class Car < ActiveRecord::Base
  belongs_to :owner, class_name: "Person", foreign_key: "owner_id"
  has_many :place_rents
  validates :owner, presence: true
  validates :registration_number, presence: true
  validates :model, presence: true
end

person.rb

class Person < ActiveRecord::Base
  has_many :cars, foreign_key: "owner_id"
  has_many :parkings, foreign_key: "owner_id"
  validates :first_name, presence: true
end

种子.rb

people = Person.create([{first_name: "Emmanuel", last_name: "Hayford"}, {first_name: "Steve", last_name: "Jobs"}, 
  {first_name: "James", last_name: "Bond"}, {first_name: "Michael", last_name: "Jordan"}])

addresses = Address.create([{street: "Limanowskiego", city: "Kraków", zip_code: "98-734"}, {street: "Zeromskiego", 
  city: "Wrocław", zip_code: "45-622"}, {street: "Chopin", city: "Gdańsk", zip_code: "98-734"}, 
  {street: "Putin", city: "Opole", zip_code: "90-938"}])

cars = Car.create([{owner: Person.first, model: "BMW", registration_number: "UJ8483"}, {owner: Person.second, model: "Lambo", registration_number: "JH4857"}, 
  {owner: Person.second, model: "Jaguar", registration_number: "DW3455"}, { owner: Person.second, model: "Ferrari", registration_number: "KP8734"}, 
  {owner: Person.first,model: "Bugatti Veyron", registration_number: "ZK9837"}])

parkings = Parking.create([{kind: "indoor", hour_price: 45.50, day_price: 200, places: 5, owner: Person.first, address_id: Address.first}, 
  {kind: "outdoor", hour_price: 20.50, day_price: 150.00, places: 10, owner: Person.second, address_id: Address.second}, 
  {kind: "street", hour_price: 270.50, day_price: 89.45, places: 7, owner: Person.second, address_id: Address.second}, 
  {kind: "private", hour_price: 40.50, day_price: 30.45, places: 2, owner: Person.find(2), address_id: Address.find(2)},])

place_rents = PlaceRent.create([{parking: Parking.first, car: Car.first, start_date: Time.now, end_date: Time.now + 3.days, price: 30}])

2.1.4 :023 > Car.all
  Car Load (0.5ms)  SELECT "cars".* FROM "cars"
 => #<ActiveRecord::Relation [#<Car id: 1, registration_number: "UJ8483", model: "BMW", owner_id: 1, created_at: "2014-11-10 00:10:26", updated_at: "2014-11-10 00:10:26">, #<Car id: 2, registration_number: "JH4857", model: "Lambo", owner_id: 2, created_at: "2014-11-10 00:10:26", updated_at: "2014-11-10 00:10:26">, #<Car id: 3, registration_number: "DW3455", model: "Jaguar", owner_id: 2, created_at: "2014-11-10 00:10:26", updated_at: "2014-11-10 00:10:26">, #<Car id: 4, registration_number: "KP8734", model: "Ferrari", owner_id: 2, created_at: "2014-11-10 00:10:26", updated_at: "2014-11-10 00:10:26">, #<Car id: 5, registration_number: "ZK9837", model: "Bugatti Veyron", owner_id: 1, created_at: "2014-11-10 00:10:26", updated_at: "2014-11-10 00:10:26">]> 
2.1.4 :024 > Car.find_by(owner_id: 1)
  Car Load (0.6ms)  SELECT  "cars".* FROM "cars"  WHERE "cars"."owner_id" = 1 LIMIT 1
 => #<Car id: 1, registration_number: "UJ8483", model: "BMW", owner_id: 1, created_at: "2014-11-10 00:10:26", updated_at: "2014-11-10 00:10:26"> 
2.1.4 :025 > 

【问题讨论】:

  • 你能发布你的seed.rb吗?
  • Rails3 的 ActiveRecord 中没有 find_by,所以我将您的标签切换为指定 Rails4(确实有 find_by)。

标签: ruby-on-rails ruby sqlite activerecord ruby-on-rails-4


【解决方案1】:

您误解了find_by 的工作原理:

find_by(*args)

查找符合指定条件的第一条记录。

find_by 仅从数据库中获取一条记录,因此它生成的 SQL 中的 LIMIT 1。如果你想找到几样东西,那就用where

Car.where(owner_id: 1)

或者,既然您已经建立了关联,请使用Person#cars

person = Person.find(1)
cars   = person.cars

【讨论】:

    【解决方案2】:

    Car.find_by_owner_id(1)Car.where(owner_id: 1).first 相同,所以基本上它返回一条记录而不是记录数组。 Car.where(owner_id: 1) 会得到你的两条记录。

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 2017-07-24
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多