【发布时间】:2011-10-18 19:24:14
【问题描述】:
大家好,我在 Mongoid 中有一个关系,我无法将 current_user 添加到此关系中以获取创建交易的用户。与 3 模型的关系。
我有三个模型 user.rb、house.rb 和 deal.rb
user.rb 关系(设计模型)
# Relationships
has_many :houses, dependent: :destroy
has_many :deals, dependent: :destroy
key :title
house.rb
# Relationships
belongs_to :user
embeds_many :deals
deal.rb
# Relationships
embedded_in :house, :inverse_of => :deals
belongs_to :user
在我的 routes.rb
resources :houses do
resources :deals
end
在我的 house_controller.rb 的 create 方法中,我为这边的每个房子获取 current_user:
def create
#@house = House.new(params[:house])
@house = current_user.houses.new(params[:house])
respond_to do |format|
if @house.save
format.html { redirect_to @house, notice: 'House was successfully created.' }
format.json { render json: @house, status: :created, location: @house }
else
format.html { render action: "new" }
format.json { render json: @house.errors, status: :unprocessable_entity }
end
end
end
在我的 deals_controller.rb 我有创建的方法:
def create
@house = House.find_by_slug(params[:house_id])
@user = User.find(:user_id)
@deal = @house.deals.create!(params[:deal])
redirect_to @house, :notice => "Comment created!"
end
如何将创建交易的 current_user 添加到最后一个方法中?
【问题讨论】:
标签: ruby-on-rails mongodb devise mongoid