ruby-on-rails - ActiveRecord 未检索所有模型

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

我有 2 个模型,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

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

enter image description here

最佳答案

你误解了如何find_by作品:

find_by(*args)

Finds the first record matching the specified conditions.

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

Car.where(owner_id: 1)

或者,因为您已经设置了关联,所以使用 Person#cars:

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

关于ruby-on-rails - ActiveRecord 未检索所有模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26835116/

相关文章:

ruby-on-rails - ASP.NET MVC3 中的 Rails 有很多等价物

php - PDOstatement有问题,其中不包含应包含的内容

javascript - 如何在客户端管理当前用户 session ?

ruby-on-rails - 如何强制我的插件随每个请求重新加载?

ruby - 将函数存储在散列中键的值中

ruby - 制作环境变量的索引哈希

java - 从java代码将静态数据插入SQLite DB

android - 如何在 Android 应用程序中添加和打开 sqlite 文件?

ruby-on-rails - Rails - 双向 "friendship"模型(续)

ruby-on-rails - rails : Find relations on relations