I am trying to do a webpage for my project at school where a user can list out the stocks that he or she has and can check the weekly prices ( I know, not the best frequency of updates)
I have already managed to get chartkick
and charjs
to sort of work in my rails application, but I realised am missing something. I tried to map out my models to display the price changes.
Currently I have following models:
User
has_many :investment_stocks
has_many :investment_price
Investment_stock
belongs_to :user
belongs_to :investment_price
belongs_to :advisor
Investment_price
has_many :investment_stocks
has_many :users, through: :investment_stocks
Migration
Investment_price
t.string :name
t.string :price
t.date :date
My idea is to manually change the investment_price.price
for each of the stocks that can be referenced and displayed on the charts. But with a user having many stocks, I am wondering if my tables need something else as the way chartkick
and chartjs
works is that I seem to need 3 variables to be displayed on the chart but yet i always seem to be able to work around 2 only
Edited
So now I have the following
User model
has_many :investment_stocks
has_many :investment_prices
Investment_stock model
belongs_to :user
has_many :investment_prices, dependent: :destroy
accepts_nested_attributes_for :investment_prices
Investment_price model
belongs_to :investment_stock, optional: true
belongs_to :user, optional: true
But I am having difficulty in adding in a nested form to create on investment_stock and investment_price tables a user_id
investment_stocks controller
def new
@investment_stock = InvestmentStock.new
@investment_stock.investment_prices.build
end
def create
@investment_stock= InvestmentStock.new(investment_stock_params)
@investment_stock["user_id"]= current_user.id
How do I make it such that the user_id appears on both the stock and price tables?