Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, after the lengthy discussion in the comments, this is what I believe you need:</p> <pre><code># app/models/user.rb class User &lt; 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 devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :timeoutable # You'll need to add a new column to your users table, called `company_id` belongs_to :company end # app/models/company.rb class Company &lt; ActiveRecord::Base # has_many should _always_ be a pluralised version of the class you're associating. This way, you # don't need to explicitly specify the class/foreign_keys has_many :products # Also, you may as well link back to your Users has_many :users end # app/models/product.rb class Product &lt; ActiveRecord::Base belongs_to :company validates :brand, :presence =&gt; true end # app/controllers/products_controller.rb def create # and now, to associate the product with the company, all you need to do is this: @product = current_user.company.products.build(params[:product]) if @product.save # redirect else # render and show errors end end </code></pre> <p>Rails relies <em>heavily</em> on convention. You really, <em>really</em> should follow them to make your life easier.</p> <p>Instead of your manual linking of <code>User</code> and <code>Company</code>, I've suggested that you use a proper association.</p> <p>That way, inside your Controller, all you need to do is call <code>current_user.company</code> to get the company, then use the <code>build</code> method on the association, and Rails will automatically handle assigning foreign keys.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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