ActiveRecord无法检索所有模型

时间:2014-11-10 00:40:54

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

我有两个型号,Car和Person。每个人都可以拥有很多车。我已经从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

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

enter image description here

2 个答案:

答案 0 :(得分:4)

你误解了find_by的工作原理:

  

<强> find_by(*参数)

     

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

find_by只从数据库中获取一条记录,因此它生成的SQL中为LIMIT 1。如果您想找到几件事,请使用where

Car.where(owner_id: 1)

或者,由于您已设置关联,请使用Person#cars

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

答案 1 :(得分:1)

Car.find_by_owner_id(1)Car.where(owner_id: 1).first相同,所以基本上它返回一条记录而不是一个记录数组。 Car.where(owner_id: 1)会为您提供两条记录。