Note that there are some explanatory texts on larger screens.

plurals
  1. POforeign_key is always 1
    text
    copied!<p>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:</p> <pre><code>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 </code></pre> <p>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...</p> <p>Edit, The models: User Model:</p> <pre><code>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, :company validates :company, :presence =&gt; 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 </code></pre> <p>Company Model:</p> <pre><code>class Company &lt; ActiveRecord::Base has_many :product, :foreign_key =&gt; 'company_id', :class_name =&gt; 'Product' end </code></pre> <p>Product Model</p> <pre><code>class Product &lt; ActiveRecord::Base belongs_to :company validates :brand, :presence =&gt; true end </code></pre>
 

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