User model has a Project model, which has photo attachment using paperclip
I don't recall ever having an issue like this before. The problem is, the attachment is being saved to the file system, but
not the database.
e.g. RAILS_ROOT/public/system/photos/id/style/file is being created, but User.find(1).projects[0].photo? returns false
User model:
has_many :projects, :dependent => :destroy
Project model:
attr_accessible :title, :content, :src, :medium, :photo attr_accessor :photo_file_name attr_accessor :photo_content_type attr_accessor :photo_file_size attr_accessor :photo_updated_at belongs_to :user has_attached_file :photo, :styles => {:medium => '800x600>', :thumb => '100x100>'}
projects_controller:
def create @user = current_user @project = Project.create! do |p| p.user_id = @user.id p.medium = params[:project][:medium] p.title = params[:project][:title] p.content = params[:project][:content] p.src = params[:project][:src] p.photo = params[:project][:photo] end if @project.save redirect_to projects_path else render :action => 'new' end end
project's show action:
- form_for(@project, :html => { :multipart => true }) do |f| ...
I usually would have "resources :projects" in my routes file, but for some reason that was messing up my custom routes (/projects was mapping to the show action instead of index, and form submissions were being processed by the index action) so I removed the resources line. The project-user association is working correctly, but the photo is not being assigned to the project. I am not sure why.