single model to have two file attachment columns - Rails 4 + Paperclip
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
add a comment |
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
add a comment |
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
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
ruby-on-rails ruby ruby-on-rails-4 paperclip
edited Nov 20 '18 at 8:45
asked Nov 19 '18 at 18:06
opensource-developer
53411031
53411031
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
edited Nov 20 '18 at 9:56
answered Nov 20 '18 at 6:17
ray
1,134213
1,134213
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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