如何在 Rails 中创建租金
How to create a Rent in Rails
我正在尝试开发一个用户可以登录、创建产品的租赁系统,其他用户可以租用他的产品。
我的问题是,我不知道如何创建检索 product_id
和 customer_id
的租金。我建立了关系,但没有用。
我还为每个人创建了 CRUD,甚至是租金。如何存储信息并传递给 Rent?
我有 4 个模型:
User
Product
Rent
Category
我在 Rent 中创建了一个名为 customer_id
的新列,并且我已经通过 class "User":
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
belongs_to :rents
class User < ActiveRecord::Base
has_many :products
has_many :rents
has_many :customer_id, :through => :rent
class Rent < ActiveRecord::Base
belongs_to :user
belongs_to :product
belongs_to :customer, :class_name => "User"
end
我想我需要创建一个按钮来检索我需要的信息。我搜索了文档,但找不到它。
这一行:has_many :customer_id, :through => :rent
在 Rails 中永远不会这样理解。如果你说 has_many :customer_id
,你犯了两个错误:
has_many
后面写什么都应该是复数。
- 如果您在
has_many
之后写的内容与型号名称不直接对应,您必须明确提及 class_name
.
你在重复同样的错误:
class Product < ActiveRecord::Base
belongs_to :rents # it should be rent.
end
现在谈谈您实际要实现的目标:
class Rent < ActiveRecord::Base
belongs_to :user
has_one :product
has_one :customer
end
而在Product
和Customer
表中,需要定义rent_id
为外键。你还应该提到他们每个人 belongs_to
Rent
.
我正在尝试开发一个用户可以登录、创建产品的租赁系统,其他用户可以租用他的产品。
我的问题是,我不知道如何创建检索 product_id
和 customer_id
的租金。我建立了关系,但没有用。
我还为每个人创建了 CRUD,甚至是租金。如何存储信息并传递给 Rent?
我有 4 个模型:
User
Product
Rent
Category
我在 Rent 中创建了一个名为 customer_id
的新列,并且我已经通过 class "User":
class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
belongs_to :rents
class User < ActiveRecord::Base
has_many :products
has_many :rents
has_many :customer_id, :through => :rent
class Rent < ActiveRecord::Base
belongs_to :user
belongs_to :product
belongs_to :customer, :class_name => "User"
end
我想我需要创建一个按钮来检索我需要的信息。我搜索了文档,但找不到它。
这一行:has_many :customer_id, :through => :rent
在 Rails 中永远不会这样理解。如果你说 has_many :customer_id
,你犯了两个错误:
has_many
后面写什么都应该是复数。- 如果您在
has_many
之后写的内容与型号名称不直接对应,您必须明确提及class_name
.
你在重复同样的错误:
class Product < ActiveRecord::Base
belongs_to :rents # it should be rent.
end
现在谈谈您实际要实现的目标:
class Rent < ActiveRecord::Base
belongs_to :user
has_one :product
has_one :customer
end
而在Product
和Customer
表中,需要定义rent_id
为外键。你还应该提到他们每个人 belongs_to
Rent
.