如何设置has_many关联?

时间:2018-06-13 00:45:32

标签: ruby-on-rails associations ruby-on-rails-5

所以我有PortStock模型,其中包含action: buyaction: sell枚举属性。每个PortStock可以是position: openposition: closed(也是枚举值)。

然而,关闭一个PortStock的是另一个PortStock(即PortStock.sell关闭PortStock.buy)。

所以我创建了一个名为ClosedPositions的连接模型,基本上我要做的就是在另一个PortStock关闭时记录元数据。

这是架构&我的ClosedPosition.rb模型的模型:

# == Schema Information
#
# Table name: closed_positions
#
#  id             :bigint(8)        not null, primary key
#  closer_id      :integer
#  closed_id      :integer
#  num_units      :integer
#  closed_price   :float
#  dollar_change  :float
#  percent_change :float
#  ticker         :string
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#

class ClosedPosition < ApplicationRecord
  belongs_to :closer, class_name: "PortStock", foreign_key: "closer_id"
  belongs_to :closed, class_name: "PortStock", foreign_key: "closed_id" 
end

在我的PortStock模型上,我有一个简单的has_many :closed_positions

但是,当我尝试向现有closed_position对象添加port_stock时,出现unknown attribute port_stock_id错误。

[27] pry(main)> closer = PortStock.find(8)
  PortStock Load (0.4ms)  SELECT  "port_stocks".* FROM "port_stocks" WHERE "port_stocks"."id" = $1 LIMIT $2  [["id", 8], ["LIMIT", 1]]
=> #<PortStock:0x00007fc951a47c30
 id: 8,
 portfolio_id: 1,
 stock_id: 84,
 volume: 250,
 purchase_price: 4.1,
 current_price: 4.0,
 percent_change: -2.43902439024389,
 created_at: Tue, 12 Jun 2018 00:46:09 UTC +00:00,
 updated_at: Tue, 12 Jun 2018 00:46:09 UTC +00:00,
 current_value: 1000.0,
 dollar_change: -24.9999999999999,
 total_spend: 1025.0,
 bought_on: Sat, 09 Jun 2018 00:00:00 UTC +00:00,
 action: "sell",
 position: "open",
 ticker: "KEY">

[28] pry(main)> closed = PortStock.find(5)
  PortStock Load (0.7ms)  SELECT  "port_stocks".* FROM "port_stocks" WHERE "port_stocks"."id" = $1 LIMIT $2  [["id", 5], ["LIMIT", 1]]
=> #<PortStock:0x00007fc951a55858
 id: 5,
 portfolio_id: 1,
 stock_id: 84,
 volume: 250,
 purchase_price: 4.1,
 current_price: 4.0,
 percent_change: -2.43902439024389,
 created_at: Mon, 11 Jun 2018 03:11:10 UTC +00:00,
 updated_at: Mon, 11 Jun 2018 23:28:30 UTC +00:00,
 current_value: 1000.0,
 dollar_change: -24.9999999999999,
 total_spend: 1025.0,
 bought_on: Sat, 09 Jun 2018 00:00:00 UTC +00:00,
 action: "buy",
 position: "open",
 ticker: "KEY">

[34] pry(main)> closer.closed_positions.create(closed: closed, num_units: closer.volume, closed_price: closed.stock.price, ticker: closed.ticker)
  Stock Load (1.7ms)  SELECT  "stocks".* FROM "stocks" WHERE "stocks"."id" = $1 LIMIT $2  [["id", 84], ["LIMIT", 1]]
   (0.2ms)  BEGIN
   (0.3ms)  ROLLBACK
ActiveModel::UnknownAttributeError: unknown attribute 'port_stock_id' for ClosedPosition.
from /.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activemodel-5.2.0/lib/active_model/attribute_assignment.rb:53:in `_assign_attribute'
[35] pry(main)> closer.closed_positions.create(closed_id: closed.id, num_units: closer.volume, closed_price: closed.stock.price, ticker: closed.ticker)
   (0.2ms)  BEGIN
   (0.3ms)  ROLLBACK
ActiveModel::UnknownAttributeError: unknown attribute 'port_stock_id' for ClosedPosition.
from /.rvm/rubies/ruby-2.5.1/lib/ruby/gems/2.5.0/gems/activemodel-5.2.0/lib/active_model/attribute_assignment.rb:53:in `_assign_attribute'

我该如何解决这个问题?

我接近这个权利吗?我可能搞乱了关联语法&amp;逻辑。

2 个答案:

答案 0 :(得分:1)

当您尝试创建closed_positions记录时,ActiveRecord会假定存在外键port_stock_id,在这种情况下不存在。所以尝试下面的解决方案添加有可能的关联。我添加了两个(我不知道它在当前背景下的相关性)。这样您就可以创建相关记录。

class PortStock < ApplicationRecord has_many :closed_stock_positions, foreign_key: 'closed_id', class_name: 'ClosedPosition' has_many :closer_stock_positions, foreign_key: 'closer_id', class_name: 'ClosedPosition' end

答案 1 :(得分:0)

ActiveRecord抱怨的是port_stock_id模型上没有ClosedPosition列。

所以,我只是将列closed_id重命名为port_stock_id,然后我在ClosedPosition模型中更改了我的关联:

class ClosedPosition < ApplicationRecord
  belongs_to :closed, class_name: "PortStock", foreign_key: "closed_id" 
end

要:

class ClosedPosition < ApplicationRecord
  belongs_to :closed, class_name: "PortStock", foreign_key: "port_stock_id" 
end

现在就像魅力一样。