Reputation: 33755
So I have a PortStock
model, that has action: buy
and action: sell
enum attributes. Each PortStock
can be either position: open
or position: closed
(also enum values).
However, what closes one PortStock
is another PortStock
(i.e. a PortStock.sell
closes a PortStock.buy
).
So I created a join model called ClosedPositions
, and basically what I want to do is record the meta-data around the closing of one PortStock
by the other.
This is the schema & model for my ClosedPosition.rb
model:
# == 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
On my PortStock
model, I have a simple has_many :closed_positions
.
However, when I try to add a closed_position
to an existing port_stock
object, I get an unknown attribute port_stock_id
error.
[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'
How do I fix this?
Am I approaching this right? I may be messing up the association syntax & logic.
Upvotes: 0
Views: 73
Reputation: 33755
What ActiveRecord was complaining about is that there was no port_stock_id
column on my ClosedPosition
model.
So, I simply renamed the column closed_id
to port_stock_id
, and then I changed my association in my ClosedPosition
model from:
class ClosedPosition < ApplicationRecord
belongs_to :closed, class_name: "PortStock", foreign_key: "closed_id"
end
To:
class ClosedPosition < ApplicationRecord
belongs_to :closed, class_name: "PortStock", foreign_key: "port_stock_id"
end
Works like a charm now.
Upvotes: 0
Reputation: 778
When you are trying to create closed_positions record, ActiveRecord assumes a foreign key port_stock_id
to be present which in this case is not present. So try the solution below add the has may associations. I added both (I don't know its relevance in terms of current context). This way you will be able to create associated records.
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
Upvotes: 1