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 4022234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:27:00+00:00 2026-05-20T10:27:00+00:00

I am trying to enter a foreign key value along with data collected from

  • 0

I am trying to enter a foreign key value along with data collected from a form. The form data is submitting perfectly, but the foreign key is always entering as 1. I have tried several ways of entering the info, my current create method attempt is below:

def create

    @product = Product.new(params[:product])

    @username = User.select("company").where("email= ?", current_user.email.to_s)
    @cid = User.select("id").where("company= ?", @username)

    if @username != nil    
      @product.company_id = @cid
      @product.save 
    end

end

Also, the find_by_something (and to_i) method throws up a No Such Method error, so I have used the above query syntax as a work around. If anyone can explain that as an aside…

Edit, The models: User Model:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable

  # Setup accessible (or protected) attributes
  attr_accessible :email, :password, :password_confirmation, :remember_me, :company

  validates :company, :presence => true
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :timeoutable

  after_save :correspond


  def correspond
    c = Company.find_or_create_by_name(self.company)
  end

end

Company Model:

class Company < ActiveRecord::Base

  has_many :product, :foreign_key => 'company_id', :class_name => 'Product'

end

Product Model

class Product < ActiveRecord::Base
  belongs_to :company

  validates :brand, :presence => true

end
  • 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-20T10:27:01+00:00Added an answer on May 20, 2026 at 10:27 am

    Dan’s answer above is correct, but as a simpler version of it, to create the association you’re describing you need:

    class Company < ActiveRecord::Base
      has_many :products
      has_many :users
    end
    
    class Product < ActiveRecord::Base
      belongs_to :company
    end
    
    class User < ActiveRecord::Base
      belongs_to :company
    end
    

    The Product and User tables need a column called company_id.

    That’s it! Now ActiveRecord will associate the objects intelligently on your behalf, and you can do things like:

    @product = Product.new(params[:product])
    @product.company = current_user.company
    @product.save
    

    The best way to understand how all these relationships work is to play with them in the console. Try things like:

    Company.first.products
    Company.find(2).products
    Company.find_by_name('Acme').products.order('price DESC')
    User.last.products.where(...)
    

    and so on…

    Lastly, a thought: You would benefit immensely from reading an introductory book on Rails. I recommend ‘Beginning Rails 3‘. It’s a fast read, you could work through it in a weekend, and it will make the big picture of how and WHY rails works the way it works very clear to you. The time spent reading the book will QUADRUPLE your productivity and the speed at which you learn more advanced stuff, because you’ll be starting with a solid foundation and approaching problems “the rails way.”

    Your problem today was definitely a case of “you’re doing it wrong”, which doesn’t mean there’s anything wrong with your logic, just that you’re making a simple problem much more difficult by trying to reinvent the wheel unnecessarily. Learning “the rails way” will make you much more productive, and will make the whole thing a lot more fun.

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

Sidebar

Related Questions

I'm trying to prevent double submission using the enter key on my form, but
Well I am trying to submit a form by pressing enter but not displaying
I'm trying to enter a little bit of HTML into an ASP.NET Dynamic Data
Trying to get jQuery to detect enter input, but space and other keys are
I am trying to use selenium to enter data into google docs so that
I'm trying to run a method when the enter key has been pressed. This
When trying to enter a SQL query with parameters using the Oracle OLE DB
I am trying to integrate the function into a map where you can enter
I am trying to create a simple page that enters data in to a
I'm trying to write a C# program, where when a user enters some data

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.