Rails carrierwave has_many error -


each user has_one character. each character has_one :profilepicture, of class picturething, holds carrierwave mount_uploader upload singe picture. each character has_many :standardpictures, of class picturething. picture upload handled in views/users/edit, hits update_pictures method in users_controller.

the idea upload 1 standardpicture @ time. seems work, rails console > picturething.all shows new picturething has been added database, , correctly displayed on page. intended 1 of character.standardpictures.

the weird thing is, somehow in whole process, character's :profilepicture set same picture uploaded. don't understand how happening. @ no point have code saying "@character.profilepicture = standardpicture", somehow has decided both first :standardpicture , :profilepicture 1 , same. if profilepicture exists, shouldn't yet, displayed on edit.html.erb page, have line <% if @character.profilepicture.nil? %>. displays uploaded picture here, profilepicture not nil, should be.

how happening?

character.rb:

has_many :standardpictures, class_name: "picturething", dependent: :destroy accepts_nested_attributes_for :standardpictures has_one  :profilepicture, class_name: "picturething", dependent: :destroy accepts_nested_attributes_for :profilepicture 

picturething.rb:

class picturething < activerecord::base   belongs_to      :character   mount_uploader  :picture, characterpicuploader   validate        :picture_size end 

app/views/users/edit.html.erb:

<%= form_for :standardpicture, url: update_pictures_user_path,               method: :post, html: { multipart: true } |f| %>   <%= f.label :picture %>   <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png' %>   <%= f.submit "upload pictures", class: "btn btn-primary" %> <% end %> 

routes.rb:

post '/users/:callsign/update_pictures', to: 'users#update_pictures',  as: :update_pictures_user 

users_controller.rb:

def update_pictures   @character = character.find_by(callsign: params[:callsign])   @user = @character.sociable   @standardpicture = @character.standardpictures.build(update_pictures_user_params)   if @standardpicture.save     flash[:success] = "pictures updated"     redirect_to(edit_user_path(@user))   else     redirect_to(edit_user_path(@user))   end end # update_pictures  def update_pictures_user_params   params.require(:standardpicture).permit(:picture) end 

back app/views/users/edit.html.erb:

<% if @character.profilepicture.nil? %>   <p>select picture below use profile picture</p> <% else %>   <%= image_tag @character.profilepicture.picture %> <% end %> 

solved. relationship between character.rb , picturething.rb not specified. fixes it:

character.rb:

has_many :standardpictures, class_name: "picturething",                             inverse_of: :character,                             foreign_key: "character_standard_id",                             dependent: :destroy accepts_nested_attributes_for :standardpictures  has_one  :profilepicture,   class_name: "picturething",                             inverse_of: :character,                             foreign_key: "character_profile_id",                             dependent: :destroy 

picturething.rb:

belongs_to      :character, class_name: "character",                             inverse_of: :standardpictures,                             foreign_key: :character_standard_id belongs_to      :character, class_name: "character",                             inverse_of: :profilepicture,                             foreign_key: :character_profile_id 

plus migration add :character_standard_id , :character_profile_id picturething in database.

the interesting thing picturething requires 2 columns, :character_standard_id , :character_profile_id, each column ever contain same value. second column seems redundant. tried working single column, couldn't. whether due being impossible or due incompetence not clear (i suspect latter), know: can problem solved single id column in picturething database?


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -