single model to have two file attachment columns - Rails 4 + Paperclip












1














I have a custom model which uses attachment model in rails.
My attachment model looks something like this



class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
has_attached_file :file, styles: { logo: ['200x50>',:png] }
end


and the other model that uses the attachment looks something like this



class User < ActiveRecord::Base
has_many :attachments, as: :attachable, dependent: :destroy
end


I want user model to have another attachment different from the one I have already setup to upload logo, something like this



has_one :user_logo, -> {where attachable_type: "UserLogo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy


but when I try to access attachment.attachable I get



undefined UserLogo as **UserLogo** is not a model.



Can anyone please suggest what changes can I make so that attachment.attachable works for both attachment type.



so for example



when i execute something like this



att = Attachment.find(3) #assume it returns attachable type as User
so att.attachable returns user object



but when i execute
att = Attachment.find(3) #assume it returns attachable type as UserLogo
so att.attachable returns exception wrong constant name UserLogo



how can i access User object from both attachment types. Thanks










share|improve this question





























    1














    I have a custom model which uses attachment model in rails.
    My attachment model looks something like this



    class Attachment < ActiveRecord::Base
    belongs_to :attachable, polymorphic: true
    has_attached_file :file, styles: { logo: ['200x50>',:png] }
    end


    and the other model that uses the attachment looks something like this



    class User < ActiveRecord::Base
    has_many :attachments, as: :attachable, dependent: :destroy
    end


    I want user model to have another attachment different from the one I have already setup to upload logo, something like this



    has_one :user_logo, -> {where attachable_type: "UserLogo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy


    but when I try to access attachment.attachable I get



    undefined UserLogo as **UserLogo** is not a model.



    Can anyone please suggest what changes can I make so that attachment.attachable works for both attachment type.



    so for example



    when i execute something like this



    att = Attachment.find(3) #assume it returns attachable type as User
    so att.attachable returns user object



    but when i execute
    att = Attachment.find(3) #assume it returns attachable type as UserLogo
    so att.attachable returns exception wrong constant name UserLogo



    how can i access User object from both attachment types. Thanks










    share|improve this question



























      1












      1








      1







      I have a custom model which uses attachment model in rails.
      My attachment model looks something like this



      class Attachment < ActiveRecord::Base
      belongs_to :attachable, polymorphic: true
      has_attached_file :file, styles: { logo: ['200x50>',:png] }
      end


      and the other model that uses the attachment looks something like this



      class User < ActiveRecord::Base
      has_many :attachments, as: :attachable, dependent: :destroy
      end


      I want user model to have another attachment different from the one I have already setup to upload logo, something like this



      has_one :user_logo, -> {where attachable_type: "UserLogo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy


      but when I try to access attachment.attachable I get



      undefined UserLogo as **UserLogo** is not a model.



      Can anyone please suggest what changes can I make so that attachment.attachable works for both attachment type.



      so for example



      when i execute something like this



      att = Attachment.find(3) #assume it returns attachable type as User
      so att.attachable returns user object



      but when i execute
      att = Attachment.find(3) #assume it returns attachable type as UserLogo
      so att.attachable returns exception wrong constant name UserLogo



      how can i access User object from both attachment types. Thanks










      share|improve this question















      I have a custom model which uses attachment model in rails.
      My attachment model looks something like this



      class Attachment < ActiveRecord::Base
      belongs_to :attachable, polymorphic: true
      has_attached_file :file, styles: { logo: ['200x50>',:png] }
      end


      and the other model that uses the attachment looks something like this



      class User < ActiveRecord::Base
      has_many :attachments, as: :attachable, dependent: :destroy
      end


      I want user model to have another attachment different from the one I have already setup to upload logo, something like this



      has_one :user_logo, -> {where attachable_type: "UserLogo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy


      but when I try to access attachment.attachable I get



      undefined UserLogo as **UserLogo** is not a model.



      Can anyone please suggest what changes can I make so that attachment.attachable works for both attachment type.



      so for example



      when i execute something like this



      att = Attachment.find(3) #assume it returns attachable type as User
      so att.attachable returns user object



      but when i execute
      att = Attachment.find(3) #assume it returns attachable type as UserLogo
      so att.attachable returns exception wrong constant name UserLogo



      how can i access User object from both attachment types. Thanks







      ruby-on-rails ruby ruby-on-rails-4 paperclip






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 8:45







      opensource-developer

















      asked Nov 19 '18 at 18:06









      opensource-developeropensource-developer

      53411031




      53411031
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Keep your attachable type 'User' which will be clean polymorphic. Define type field inside 'Attachment' model having two values logo & file



          Association will get updated as below



          class User < ActiveRecord::Base

          has_many :attachments, -> {where type: "file"}, as: :attachable, dependent: :destroy
          has_one :user_logo, -> {where type: "logo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy

          end


          I suggest you to have different styles for attachment depends on what type it have (logo/file). Validation for attachment type also vary as per type.



          has_attached_file :file, styles: ->(file){ file.instance.get_styles }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'logo' }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'file' }


          def get_styles
          if self.type == 'logo'
          style1
          elsif self.type == 'file'
          style2
          end
          end


          Please update status or any query you u get further.



          Update - answering to further question



          First Way: If you are using UserLogo as an attachable_type in Attachment, then it do not follow polymorphic association so define custom association.



          belongs_to :resource,
          -> { |attachment| ['User', 'UserLogo'].include? attachment.attachable },
          class_name: 'User',
          foreign_key: :attachable_id

          belongs_to :belongs_to :attachable,
          -> { |attachment| ['User', 'UserLogo'].exclude? attachment.attachable_type },
          class_name: :attachable_type,
          foreign_key: :attachable_id


          Second Way: Create UserLogo class extending User class. It will find UserLogo with same User data






          share|improve this answer























          • Hi @ray, thanks for your reply. I still get a exception, i have updated the question with sample example, can you please check? Thanks
            – opensource-developer
            Nov 20 '18 at 8:38










          • so my main question is how can i access user object when attachment type is logo
            – opensource-developer
            Nov 20 '18 at 8:47










          • @opensource-developer I have not tried so not pretty sure, but it should work, check update in my answer
            – ray
            Nov 20 '18 at 9:56










          • Thanks @ray, the second way worked for me :)
            – opensource-developer
            Nov 20 '18 at 16:47










          • @opensource-developer my pleasure :)
            – ray
            Nov 21 '18 at 6:46











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380335%2fsingle-model-to-have-two-file-attachment-columns-rails-4-paperclip%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Keep your attachable type 'User' which will be clean polymorphic. Define type field inside 'Attachment' model having two values logo & file



          Association will get updated as below



          class User < ActiveRecord::Base

          has_many :attachments, -> {where type: "file"}, as: :attachable, dependent: :destroy
          has_one :user_logo, -> {where type: "logo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy

          end


          I suggest you to have different styles for attachment depends on what type it have (logo/file). Validation for attachment type also vary as per type.



          has_attached_file :file, styles: ->(file){ file.instance.get_styles }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'logo' }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'file' }


          def get_styles
          if self.type == 'logo'
          style1
          elsif self.type == 'file'
          style2
          end
          end


          Please update status or any query you u get further.



          Update - answering to further question



          First Way: If you are using UserLogo as an attachable_type in Attachment, then it do not follow polymorphic association so define custom association.



          belongs_to :resource,
          -> { |attachment| ['User', 'UserLogo'].include? attachment.attachable },
          class_name: 'User',
          foreign_key: :attachable_id

          belongs_to :belongs_to :attachable,
          -> { |attachment| ['User', 'UserLogo'].exclude? attachment.attachable_type },
          class_name: :attachable_type,
          foreign_key: :attachable_id


          Second Way: Create UserLogo class extending User class. It will find UserLogo with same User data






          share|improve this answer























          • Hi @ray, thanks for your reply. I still get a exception, i have updated the question with sample example, can you please check? Thanks
            – opensource-developer
            Nov 20 '18 at 8:38










          • so my main question is how can i access user object when attachment type is logo
            – opensource-developer
            Nov 20 '18 at 8:47










          • @opensource-developer I have not tried so not pretty sure, but it should work, check update in my answer
            – ray
            Nov 20 '18 at 9:56










          • Thanks @ray, the second way worked for me :)
            – opensource-developer
            Nov 20 '18 at 16:47










          • @opensource-developer my pleasure :)
            – ray
            Nov 21 '18 at 6:46
















          2














          Keep your attachable type 'User' which will be clean polymorphic. Define type field inside 'Attachment' model having two values logo & file



          Association will get updated as below



          class User < ActiveRecord::Base

          has_many :attachments, -> {where type: "file"}, as: :attachable, dependent: :destroy
          has_one :user_logo, -> {where type: "logo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy

          end


          I suggest you to have different styles for attachment depends on what type it have (logo/file). Validation for attachment type also vary as per type.



          has_attached_file :file, styles: ->(file){ file.instance.get_styles }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'logo' }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'file' }


          def get_styles
          if self.type == 'logo'
          style1
          elsif self.type == 'file'
          style2
          end
          end


          Please update status or any query you u get further.



          Update - answering to further question



          First Way: If you are using UserLogo as an attachable_type in Attachment, then it do not follow polymorphic association so define custom association.



          belongs_to :resource,
          -> { |attachment| ['User', 'UserLogo'].include? attachment.attachable },
          class_name: 'User',
          foreign_key: :attachable_id

          belongs_to :belongs_to :attachable,
          -> { |attachment| ['User', 'UserLogo'].exclude? attachment.attachable_type },
          class_name: :attachable_type,
          foreign_key: :attachable_id


          Second Way: Create UserLogo class extending User class. It will find UserLogo with same User data






          share|improve this answer























          • Hi @ray, thanks for your reply. I still get a exception, i have updated the question with sample example, can you please check? Thanks
            – opensource-developer
            Nov 20 '18 at 8:38










          • so my main question is how can i access user object when attachment type is logo
            – opensource-developer
            Nov 20 '18 at 8:47










          • @opensource-developer I have not tried so not pretty sure, but it should work, check update in my answer
            – ray
            Nov 20 '18 at 9:56










          • Thanks @ray, the second way worked for me :)
            – opensource-developer
            Nov 20 '18 at 16:47










          • @opensource-developer my pleasure :)
            – ray
            Nov 21 '18 at 6:46














          2












          2








          2






          Keep your attachable type 'User' which will be clean polymorphic. Define type field inside 'Attachment' model having two values logo & file



          Association will get updated as below



          class User < ActiveRecord::Base

          has_many :attachments, -> {where type: "file"}, as: :attachable, dependent: :destroy
          has_one :user_logo, -> {where type: "logo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy

          end


          I suggest you to have different styles for attachment depends on what type it have (logo/file). Validation for attachment type also vary as per type.



          has_attached_file :file, styles: ->(file){ file.instance.get_styles }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'logo' }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'file' }


          def get_styles
          if self.type == 'logo'
          style1
          elsif self.type == 'file'
          style2
          end
          end


          Please update status or any query you u get further.



          Update - answering to further question



          First Way: If you are using UserLogo as an attachable_type in Attachment, then it do not follow polymorphic association so define custom association.



          belongs_to :resource,
          -> { |attachment| ['User', 'UserLogo'].include? attachment.attachable },
          class_name: 'User',
          foreign_key: :attachable_id

          belongs_to :belongs_to :attachable,
          -> { |attachment| ['User', 'UserLogo'].exclude? attachment.attachable_type },
          class_name: :attachable_type,
          foreign_key: :attachable_id


          Second Way: Create UserLogo class extending User class. It will find UserLogo with same User data






          share|improve this answer














          Keep your attachable type 'User' which will be clean polymorphic. Define type field inside 'Attachment' model having two values logo & file



          Association will get updated as below



          class User < ActiveRecord::Base

          has_many :attachments, -> {where type: "file"}, as: :attachable, dependent: :destroy
          has_one :user_logo, -> {where type: "logo"}, class_name: "Attachment", foreign_key: :attachable_id, foreign_type: :attachable_type, dependent: :destroy

          end


          I suggest you to have different styles for attachment depends on what type it have (logo/file). Validation for attachment type also vary as per type.



          has_attached_file :file, styles: ->(file){ file.instance.get_styles }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'logo' }

          validates_attachment_content_type :file, :content_type: [..], if: -> { type == 'file' }


          def get_styles
          if self.type == 'logo'
          style1
          elsif self.type == 'file'
          style2
          end
          end


          Please update status or any query you u get further.



          Update - answering to further question



          First Way: If you are using UserLogo as an attachable_type in Attachment, then it do not follow polymorphic association so define custom association.



          belongs_to :resource,
          -> { |attachment| ['User', 'UserLogo'].include? attachment.attachable },
          class_name: 'User',
          foreign_key: :attachable_id

          belongs_to :belongs_to :attachable,
          -> { |attachment| ['User', 'UserLogo'].exclude? attachment.attachable_type },
          class_name: :attachable_type,
          foreign_key: :attachable_id


          Second Way: Create UserLogo class extending User class. It will find UserLogo with same User data







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 9:56

























          answered Nov 20 '18 at 6:17









          rayray

          1,380219




          1,380219












          • Hi @ray, thanks for your reply. I still get a exception, i have updated the question with sample example, can you please check? Thanks
            – opensource-developer
            Nov 20 '18 at 8:38










          • so my main question is how can i access user object when attachment type is logo
            – opensource-developer
            Nov 20 '18 at 8:47










          • @opensource-developer I have not tried so not pretty sure, but it should work, check update in my answer
            – ray
            Nov 20 '18 at 9:56










          • Thanks @ray, the second way worked for me :)
            – opensource-developer
            Nov 20 '18 at 16:47










          • @opensource-developer my pleasure :)
            – ray
            Nov 21 '18 at 6:46


















          • Hi @ray, thanks for your reply. I still get a exception, i have updated the question with sample example, can you please check? Thanks
            – opensource-developer
            Nov 20 '18 at 8:38










          • so my main question is how can i access user object when attachment type is logo
            – opensource-developer
            Nov 20 '18 at 8:47










          • @opensource-developer I have not tried so not pretty sure, but it should work, check update in my answer
            – ray
            Nov 20 '18 at 9:56










          • Thanks @ray, the second way worked for me :)
            – opensource-developer
            Nov 20 '18 at 16:47










          • @opensource-developer my pleasure :)
            – ray
            Nov 21 '18 at 6:46
















          Hi @ray, thanks for your reply. I still get a exception, i have updated the question with sample example, can you please check? Thanks
          – opensource-developer
          Nov 20 '18 at 8:38




          Hi @ray, thanks for your reply. I still get a exception, i have updated the question with sample example, can you please check? Thanks
          – opensource-developer
          Nov 20 '18 at 8:38












          so my main question is how can i access user object when attachment type is logo
          – opensource-developer
          Nov 20 '18 at 8:47




          so my main question is how can i access user object when attachment type is logo
          – opensource-developer
          Nov 20 '18 at 8:47












          @opensource-developer I have not tried so not pretty sure, but it should work, check update in my answer
          – ray
          Nov 20 '18 at 9:56




          @opensource-developer I have not tried so not pretty sure, but it should work, check update in my answer
          – ray
          Nov 20 '18 at 9:56












          Thanks @ray, the second way worked for me :)
          – opensource-developer
          Nov 20 '18 at 16:47




          Thanks @ray, the second way worked for me :)
          – opensource-developer
          Nov 20 '18 at 16:47












          @opensource-developer my pleasure :)
          – ray
          Nov 21 '18 at 6:46




          @opensource-developer my pleasure :)
          – ray
          Nov 21 '18 at 6:46


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380335%2fsingle-model-to-have-two-file-attachment-columns-rails-4-paperclip%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          ts Property 'filter' does not exist on type '{}'

          mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window