Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7749657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:05:48+00:00 2026-06-01T11:05:48+00:00

I have the following models: Post.rb class Post < ActiveRecord::Base belongs_to :category has_many :attachments,

  • 0

I have the following models:

Post.rb

  class Post < ActiveRecord::Base

  belongs_to :category
  has_many :attachments, :dependent => :destroy
  has_many :citations, :dependent => :destroy

  validates :title, :category_id, :content, :presence =>true

  acts_as_taggable_on :keywords
  accepts_nested_attributes_for :attachments, :allow_destroy => true, 
         :reject_if => proc { |attributes| attributes['photo'].blank?}
  accepts_nested_attributes_for :citations, :allow_destroy=>true

end

Attachment.rb

  class Attachment < ActiveRecord::Base

  belongs_to :post

  has_attached_file :photo, :styles => { :medium => "637x471>", 
                :thumb => Proc.new { |instance| instance.resize },
                :carousel => Proc.new { |instance| instance.decide_style }
                },
                :url => "/pictures/:style/:basename.:extension",
                :path =>":rails_root/public/pictures/:style/:basename.:extension"


  validates_attachment_content_type :photo, :content_type => ['image/png', 'image/jpg', 'image/jpeg']                  
    validates_attachment_size :photo, :less_than => 2.megabytes         


   ### End Paperclip ####
  def decide_style

   # catid = Post.find(param[:post_category_id])
   # something here to get the selection from the form.

      if(catid == 2)
      # "400x800>" or custom style here
      end

      end


 def resize     
 geo = Paperclip::Geometry.from_file(photo.to_file(:original))

 ratio = geo.width/geo.height  

 min_width  = 142
 min_height = 119

 if ratio > 1
   # Horizontal Image
   final_height = min_height
   final_width  = final_height * ratio
   "#{final_width.round}x#{final_height.round}!"
 else
   # Vertical Image
   final_width  = min_width
   final_height = final_width * ratio
   "#{final_height.round}x#{final_width.round}!"
  end
 end 

 end

I am trying to conditionally apply a style, based on the selection made from a drop down on the form. I am just not sure as to where the decision as to what style should be chosen is implemented.

Thoughts?

Rishi

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:05:49+00:00Added an answer on June 1, 2026 at 11:05 am

    It looks like you’re trying to base it upon the category_id value on the Post model, is that right? If so, you can actually pass a lambda as the value of the :styles entry in your attachment options, which includes the attachment as an attribute.

    has_attached_file :photo,
        :styles => lambda { |attachment| {
          :medium => "637x471>", 
          :thumb => attachment.instance.resize,
          :carousel => attachment.instance.decide_style,
        } },
        :url => "/pictures/:style/:basename.:extension",
        :path =>":rails_root/public/pictures/:style/:basename.:extension"
    

    The attachment is a Paperclip::Attachment object, and calling the instance method on it returns your model instance.

    Then in your decide_style method, you can base it upon your model’s category_id value as needed.

    def decide_style
      case category_id
      when 1 then "200x400>"
      when 2 then "400x800>"
      else "50x50#"
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Models: class Topic < ActiveRecord::Base has_many :posts, :dependent => :destroy
I have the following models. class Post < ActiveRecord::Base has_many :timeline_items, :as=>:subject define_index do
I have the following two models class Post < ActiveRecord::Base has_many :comments end class
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I have a rails 3.1 app with the following Models: class Post < ActiveRecord::Base
I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have the following models with associations as given below:- class Comment < ActiveRecord::Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.