Ruby on rails Rails模型嵌套表单,如何在2个模型中创建外键
我正在尝试为我在学校的项目制作一个网页,用户可以列出他或她持有的股票,并可以查看每周价格(我知道,不是最佳更新频率) 我已经设法让Ruby on rails Rails模型嵌套表单,如何在2个模型中创建外键,ruby-on-rails,model,chartkick,Ruby On Rails,Model,Chartkick,我正在尝试为我在学校的项目制作一个网页,用户可以列出他或她持有的股票,并可以查看每周价格(我知道,不是最佳更新频率) 我已经设法让chartkick和charjs在我的rails应用程序中进行某种工作,但我意识到我遗漏了一些东西。我试图画出我的模型来显示价格的变化 目前我有以下型号: 使用者 投资股票 belongs_to :user belongs_to :investment_price belongs_to :advisor 投资价格 has_many :investment_st
chartkick
和charjs
在我的rails应用程序中进行某种工作,但我意识到我遗漏了一些东西。我试图画出我的模型来显示价格的变化
目前我有以下型号:
使用者
投资股票
belongs_to :user
belongs_to :investment_price
belongs_to :advisor
投资价格
has_many :investment_stocks
has_many :users, through: :investment_stocks
迁移
投资价格
t.string :name
t.string :price
t.date :date
我的想法是手动更改每个可以在图表上引用和显示的股票的投资价格.price
。但是,对于一个有很多股票的用户,我想知道我的表是否还需要其他东西,因为chartkick
和chartjs
的工作方式是,我似乎需要在图表上显示3个变量,但我似乎总是只能处理2个变量
编辑
现在我有以下几点
用户模型
has_many :investment_stocks
has_many :investment_prices
投资股票模型
belongs_to :user
has_many :investment_prices, dependent: :destroy
accepts_nested_attributes_for :investment_prices
投资价格模型
belongs_to :investment_stock, optional: true
belongs_to :user, optional: true
但我很难在investment\u stock和investment\u price表中添加嵌套表单来创建用户id
投资股控制员
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
如何使用户id同时出现在股票和价格表上?好的,我想您可以试试: 投资价格模型->属于:投资股票,属于:用户。
股票有价格,用户有股票和价格。请添加一个有价值的链接,使此答案更有用
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