Ruby on Rails中belongs_to关系为何失效?

2026-04-11 17:170阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计196个文字,预计阅读时间需要1分钟。

Ruby on Rails中belongs_to关系为何失效?

我有以下代码记录课+class Car ActiveRecord:Base belongs_to :ownerend 在我尝试这个代码时 Car.first.owner 给我错误未定义的方法 owner 如果我遗漏任何东西,任何人都可以让我知道,您需要什么。

我有以下活跃的记录课

class Car < ActiveRecord::Base belongs_to :owner end

在我尝试这个代码时

Car.first.owner

它给我错误“未定义的方法所有者”

如果我遗漏任何东西,任何人都可以让我现在

您需要在所有者方面编写关系:has_one:car或has_many:cars,具体取决于您的需求.

class Car < ActiveRecord::Base belongs_to :owner end class Owner < ActiveRecord::Base has_one :car end

Ruby on Rails中belongs_to关系为何失效?

本文共计196个文字,预计阅读时间需要1分钟。

Ruby on Rails中belongs_to关系为何失效?

我有以下代码记录课+class Car ActiveRecord:Base belongs_to :ownerend 在我尝试这个代码时 Car.first.owner 给我错误未定义的方法 owner 如果我遗漏任何东西,任何人都可以让我知道,您需要什么。

我有以下活跃的记录课

class Car < ActiveRecord::Base belongs_to :owner end

在我尝试这个代码时

Car.first.owner

它给我错误“未定义的方法所有者”

如果我遗漏任何东西,任何人都可以让我现在

您需要在所有者方面编写关系:has_one:car或has_many:cars,具体取决于您的需求.

class Car < ActiveRecord::Base belongs_to :owner end class Owner < ActiveRecord::Base has_one :car end

Ruby on Rails中belongs_to关系为何失效?