Note that there are some explanatory texts on larger screens.

plurals
  1. POActive Record Does Not Set Foreign Key With has_many and belongs_to does
    text
    copied!<p>Background - I have 2 models, a upload model and a user model. Originally I had owner_id in the upload model(table) as the foreign key for the id of the user in the user model. However I could not get the foreign key to work, so I decided to go the "rails " way by renaming owner_id to user_id. Even after setting the column to user_id, it does not populate with any values at all.</p> <pre><code> class User &lt; ActiveRecord::Base has_many :uploads end class Upload &lt; ActiveRecord::Base belongs_to :user end </code></pre> <p>Have tried explicitly setting the key, but it still does not populate the user_id field in the uploads table</p> <pre><code> class User &lt; ActiveRecord::Base has_many :uploads ,:foreign_key =&gt; 'user_id' end </code></pre> <p>Might be something simple but I cant seem to find what it is. any suggestions ? </p> <p>** the upload controller</p> <pre><code>class UploadsController &lt; ApplicationController def index @uploads = Upload.all end def new @upload = Upload.new end def create @upload = Upload.new(params[:upload]) if @upload.save flash[:notice] = "your file has been uploaded" redirect_to uploads_path else render :action =&gt; 'new' end end def destroy @upload = Upload.find(params[:id]) @upload.destroy flash[:notice] = "Sucessfully deleted your file" redirect_to uploads_path end def download upload = Upload.find(params[:id]) #location = "#{Rails.root}" # send_file (@upload) #send_file('public/test_file.pdf', :filename =&gt; 'Test File', :type =&gt; 'application/pdf', :disposition =&gt; 'attachment', :streaming =&gt; 'true', :buffer_size =&gt; '4096') send_file upload.uploaded.path, :filename =&gt; upload.uploaded_file_name, :type =&gt; upload.uploaded_content_type, :disposition =&gt; 'attachment' flash[:notice] = "Your file has been downloaded" end end </code></pre> <p>**The upload form</p> <pre><code>&lt;%= form_for(@upload, :html =&gt; { :multipart =&gt; true }) do |form| %&gt; &lt;form&gt; &lt;fieldset&gt; &lt;div class="clearfix"&gt; &lt;label for="fileInput"&gt;File input&lt;/label&gt; &lt;div class="input"&gt; &lt;%= form.file_field :uploaded %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;input type="submit" class="btn primary" &lt;%= form.submit "Upload" %&gt; &amp;nbsp;&lt;button type="reset" class="btn"&gt;Cancel&lt;/button&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;% end %&gt; </code></pre> <p>== Schema Information</p> <p>Table name: users</p> <p>id :integer not null, primary key</p> <p>email :string(255) default(""), not null</p> <p>encrypted_password :string(128) default(""), not null</p> <p>reset_password_token :string(255)</p> <p>reset_password_sent_at :datetime</p> <p>remember_created_at :datetime</p> <p>sign_in_count :integer default(0)</p> <p>current_sign_in_at :datetime</p> <p>last_sign_in_at :datetime</p> <p>current_sign_in_ip :string(255)**</p> <p>last_sign_in_ip :string(255)</p> <p>created_at :datetime</p> <p>updated_at :datetime</p> <p>admin :boolean default(FALSE)</p> <p>== Schema Information -----------------------------------------</p> <p>Table name: uploads</p> <p>id :integer </p> <p>created_at :datetime</p> <p>updated_at :datetime</p> <p>uploaded_file_name :string(255)</p> <p>uploaded_content_type :string(255)</p> <p>uploaded_file_size :integer</p> <p>uploaded_updated_at :datetime</p> <p>user_id :integer</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