On 9 July 2015 at 21:28, Bazley <1975jmp@gmail.com> wrote:
> Each User has_one Character. Each Character has_one :profilepicture, of
> class Picturething, which holds a Carrierwave mount_uploader to upload a
> singe picture. Each Character has_many :standardpictures, also of class
> Picturething. Picture upload is handled in views/users/edit, which hits the
> update_pictures method in users_controller.
>
> The idea is to upload one standardpicture at a time. It seems to work, Rails
> console > Picturething.all shows that a new Picturething has been added to
> the database, and it is correctly displayed on the page. This is intended to
> be one of the character.standardpictures.
>
> The weird thing is, somehow in this whole process, the character's
> :profilepicture is also set to be the same picture that was uploaded. I
> don't understand how this is happening. At no point do I have code saying
> something like "@character.profilepicture = standardpicture", but somehow it
> has decided that both the first :standardpicture and the :profilepicture are
> one and the same. If the profilepicture exists, which it shouldn't yet, it
> is displayed on the edit.html.erb page, where I have the line `<% if
> @character.profilepicture.nil? %>`. It displays the uploaded picture here,
> so clearly profilepicture is not nil, but it should be.
>
> How is this 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
You need two belongs_to here, one for each association. Use unique
names, foreign_key and class_name here and in character.rb to specify
which is which. Then obviously two _id fields.
Currently it is conflating the two relationships.
Colin
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvP8gR8wj6HKU1HEYq%3DfHtbxcEWDSDPiGzhHynZuSBWbw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment