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 8445255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:36:45+00:00 2026-06-10T09:36:45+00:00

I have a model Team and Division described as a division should have many

  • 0

I have a model Team and Division described as a division should have many teams:

class Team < ActiveRecord::Base
  belongs_to :division

  has_attached_file :logo, :styles => { :large => "120x180>", :thumb => "100x100>", :tiny => "25x25>" },  
      :url => "/assets/images/:style/teams/#{self.division.name}/:basename.:extension",  
      :path => ":rails_root/public/assets/images/teams/#{self.division.name}/:basename.:extension"
end


class Division < ActiveRecord::Base
  has_many :teams
end

I want to get the name of division of the team by using: #{self.division.name} to include in the string but it doesn’t work at all.

Error given:

NoMethodError (undefined method `division' for #<Class:0x007f80179b68f8>):

Even when I use #{self.name} to get the name of the team, but it return only the class name: "Team". In console, I call them just fine:

1.9.3p194 :009 > team = Team.find(4)
  Team Load (0.3ms)  SELECT `teams`.* FROM `teams` WHERE `teams`.`id` = 4 LIMIT 1
 => #<Team id: 4, name: "Arsenal F.C.", league_id: nil, division_id: 1, stadium_id: 2, history: "This is Arsenal F.C. history file", wins: 1, losses: 1, win_percentage: 1.0, created_at: "2012-08-25 09:25:22", updated_at: "2012-08-27 01:54:08", logo_file_size: 29303, logo_updated_at: "2012-08-27 01:54:08", logo_content_type: "image/png", logo_file_name: "arsenal.png"> 
1.9.3p194 :010 > team.name
 => "Arsenal F.C." 
1.9.3p194 :011 > team.division.name
  Division Load (0.3ms)  SELECT `divisions`.* FROM `divisions` WHERE `divisions`.`id` = 1 LIMIT 1
 => "English Premier Division" 
1.9.3p194 :012 > 

I am trying to call self.name (name of the team) or self.division.name (name of the division of the team) in team.rb, not in a controller.

Any idea why? Because of the self hasn’t loaded yet? How to solve this?

Update: I really like shioyama’s solution because we can pass Proc to paperclip. However, my rails_admin and paperclip and this solution seem still throwing error about gsub on the form. I found another thread Dynamic use of :default_url in Paperclip that will cover my problem. shioyama’s solution works fine without rails_admin.

  • 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-10T09:36:46+00:00Added an answer on June 10, 2026 at 9:36 am

    When you use self inside a class definition, it refers to the class itself, not to an instance of that class. To get at the instance you have to use a lambda:

    has_attached_file :logo,
      :styles => { :large => "120x180>", :thumb => "100x100>", :tiny => "25x25>" },  
      :url => lamda { |attachment|
        team = attachment.instance
        "/assets/images/:style/teams/#{team.division.name}/:basename.:extension"
      },  
      :path => lambda { |attachment|
        team = attachment.instance
        ":rails_root/public/assets/images/teams/#{team.division.name}/:basename.:extension"
      }
    

    attachment.instance gets the instance that the attachment is attached to (in this case an instance of the Team class). See the paperclip documentation.

    See also this answer: can you pass self to lambda in rails?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user
hi i have to following model class Match(models.Model): Team_one = models.ForeignKey('Team', related_name='Team_one') Team_two =
I have 2 models: class Team(models.Model): Team model for creating a basic team. image
I currently have a model for team.rb and user.rb, which is a many to
I have the following models defined: class Team(models.Model): Name = models.CharField(max_length=250) class Fixture(models.Model): HomeTeam
I have the following models defined: class Player(models.Model): Team = models.ForeignKey(Team) Name = models.CharField(max_length=200)
I have a very simple method: Class Team(models.Model): def sides(self): return SideNames.objects.filter(team=self) SideNames is
I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number
How to model a domain when you have a base class and 2 classes
Say I have a model like: class Team(models.Model): name = models.CharField(max_length=20) class Game(models.Model): title

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.