Dichen Qian
Dichen Qian

Reputation: 21

paperclip where does it store file id

I'm new to ror and paperclip. I use paperclip to upload file, and I wonder how does paperclip determine the file id. For example in my User model, I add paperclip attachment "has_attachment_file", then I find that in database(I use sqlite3) there are four new columns in table "User" including file name, file type, uploaded time and file size. However I can't find the file id this column as I can retrieve from user.file.id. Where does paperclip store this things?

Upvotes: 0

Views: 180

Answers (1)

doctororange
doctororange

Reputation: 11810

The attached file isn't stored in relation to the User — it's stored directly on it. That's why your User table has the extra columns, and why the file doesn't have an id.

If you want a User to have many files you will need to model them separately and use Rails' has_many. Something like:

class User < ActiveRecord::Base
  has_many :images
end

class Image < ActiveRecord::Base
  has_attached_file :file
  belongs_to :user
end

Upvotes: 1

Related Questions