Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd current_user Devise to Mongoid relations
    text
    copied!<p>Hi Guys I have a Relationships in Mongoid and I can not add current_user to this relation for get the user that create the deal. A relation with 3 model.</p> <p>I have three models user.rb, house.rb and deal.rb</p> <p><strong>user.rb</strong> Relationships (devise model)</p> <pre><code># Relationships has_many :houses, dependent: :destroy has_many :deals, dependent: :destroy key :title </code></pre> <p><strong>house.rb</strong></p> <pre><code># Relationships belongs_to :user embeds_many :deals </code></pre> <p><strong>deal.rb</strong></p> <pre><code># Relationships embedded_in :house, :inverse_of =&gt; :deals belongs_to :user </code></pre> <p>In my <strong>routes.rb</strong></p> <pre><code> resources :houses do resources :deals end </code></pre> <p>In my houses_controller.rb in my create method I get current_user for each house of this side:</p> <pre><code>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 </code></pre> <p>In my <strong>deals_controller.rb</strong> I have the created method this:</p> <pre><code> 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 =&gt; "Comment created!" end </code></pre> <p><strong>How I can add to this last method create, the current_user that created the deal?</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload