Manually updating attributes mounted by Carrierwave Uploader
Asked Answered
C

2

6

I am unable to use model.update_attribute on an attribute that is mounted by a carrierwave uploader. The SQL statement wont accept the value and adds NULL to the placeholder. If I remove the mount_uploader statement from the model class it works as normal. I am troubleshooting things from the console and trying to add some attributes while seeding the DB and this is thwarting my efforts. Ideas?

Thanks.

Update: Relevant code:

class Profile < ActiveRecord::Base
  belongs_to :user
  has_and_belongs_to_many :sports
  has_and_belongs_to_many :interests
  has_and_belongs_to_many :minors
  has_and_belongs_to_many :majors
  has_and_belongs_to_many :events
  has_and_belongs_to_many :groups
  attr_accessible :description, :username, :avatar, :bio, :first_name, :last_name, :major, :minor, :graduation_date, :living_situation, :phone, :major_ids, :minor_ids, :sport_ids
  mount_uploader :avatar, AvatarUploader
end

I am simply trying to rewrite the :avatar string from a db seed file and while testing from the rails console like so: Profile.first.update_attribute(:avatar, 'foo')

Both work when I comment out the mount_uploader line.

Does adding the mount_uploader method freeze the string or make it immutable?

Caban answered 23/9, 2013 at 0:6 Comment(1)
It would be helpful if you post the code you are trying to execute. What value are you trying to set the attribute to?Excoriate
C
11

I found a solution to this.

My issue was that I was not able to alter the attribute mounted my the CarrierWave uploader from my seeds.rb file.

This works:

user.profile.update_column(:avatar, 'foobar/image.png')
Caban answered 8/10, 2013 at 21:12 Comment(0)
R
0

The above answer makes it impossible to retrieve that information except with using pluck(:avatar) so i would not go with it

You have to set the variables on the Uploader class instead of manually on the database

Edit

The other way to retrieve the value from this solution is to do

user = User.last 
avatar_value = user[:avatar]
Renteria answered 5/7, 2023 at 10:53 Comment(2)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewRicardoricca
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Fajardo

© 2022 - 2025 — McMap. All rights reserved.