Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6704233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:15:55+00:00 2026-05-26T07:15:55+00:00

Hi Guys I have a Relationships in Mongoid and I can not add current_user

  • 0

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.

I have three models user.rb, house.rb and deal.rb

user.rb Relationships (devise model)

# 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

In my routes.rb

 resources :houses do
  resources :deals
 end

In my houses_controller.rb in my create method I get current_user for each house of this side:

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

In my deals_controller.rb I have the created method this:

    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

How I can add to this last method create, the current_user that created the deal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:15:55+00:00Added an answer on May 26, 2026 at 7:15 am

    You can simply add these two lines to the create action:

    @deal.user=current_user
    @deal.save
    

    And I would also suggest you not to use create! instead you should use .new and .save like in the scaffolded create actions! 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys have a problem don't know why this is not working, i have file
Great guys have helped me a lot in this other post: Get last word
Thus far you guys have been wildly helpful with me getting this little ditty
guys i have arrays in which i have to match this kind of text
This question regards how one can effectively create and persist event domain objects on
Guys, I have been surfing thru the web to find a way that I
Hey guys have her an code where i add an option to a html
Hey guys I have a confusing question, I have a user table and it
Guys, I've got this problem that I searched almost everywhere (maybe I don't know
Hello guys have a div that is outside of a table and I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.