【发布时间】:2012-04-28 12:12:04
【问题描述】:
我有以下 3 个模型,并希望将两个外键关联到创建帖子(user_id(来自用户模型)和 review_id(来自评论模型)到目标模型。我设法使用“current_user”关联 user_id使用下面链接中给出的解决方案来创建目标,但不确定如何为 review_id 完成这项工作。
谢谢。
Devise how to associate current user to post?
我的模型:
class User < ActiveRecord::Base
has_many :reviews
has_many :periods, :through => :reviews
has_many :goals
end
class Review < ActiveRecord::Base
belongs_to :user
belongs_to :period
has_many :goals
end
class Goal < ActiveRecord::Base
belongs_to :user
belongs_to :review
end
我的目标控制器.rb
def create
@goal = current_user.goals.build(params[:goal])
respond_to do |format|
if @goal.save
format.html { redirect_to @goal, notice: 'Goal was successfully created.' }
format.json { render json: @goal, status: :created, location: @goal }
else
format.html { render action: "new" }
format.json { render json: @goal.errors, status: :unprocessable_entity }
end
end
end
干杯, 阿兹伦
【问题讨论】:
-
你已经发布了两次
User模型。 -
再次检查。还有 2 个
class User语句。虽然你已经修复了他们的内容。
标签: ruby-on-rails