Has_one :through with :source. How to alias association?

Hi to everyone,

I have the following models:

class Attachment < ActiveRecord::Base
has_attachment :content_type => [:image, ‘application/pdf’],
:max_size => 3.megabytes,
:path_prefix => ‘…/private/attachments’,
:storage => :file_system,
:processor => :rmagick,
:thumbnails => { :small => ‘32x32’, :medium =>
‘64x64’, :large => ‘256x256’ }

validates_as_attachment

belongs_to :attachable, :polymorphic => true
end

class Maker < ActiveRecord::Base
validates_presence_of :name, :on => :create, :message => “can’t be
blank”
validates_uniqueness_of :name, :message => ‘name must be unique’
validates_presence_of :description, :on => :create, :message => “can’t
be blank”

has_one :attachment, :as => :attachable, :dependent => :destroy
has_one :logo, :through => :attachment, :source => :attachable,
:source_type => ‘Maker’, :dependent => :destroy,
:conditions => [‘attachments.content_type = ?’,
‘image/jpeg’ ]

has_many :products

def deletable?
self.products.empty?
end

end

What I would like to accomplish is to have a “logo” association on the
“Maker” model which allows
me to fetch the logo (eventually) associated to the maker.

The row:

has_one :logo, :through => :attachment, :source => :attachable,
:source_type => ‘Maker’, :dependent => :destroy,
:conditions => [‘attachments.content_type = ?’,
‘image/jpeg’ ]

produces the same results of:

has_one :another_logo, :class_name => ‘Attachment’, :foreign_key =>
‘attachable_id’, :conditions => [‘attachable_type = ?’, ‘Maker’ ]

Which one is the best approach to achieve the desired result?

Thanks in advance for your help

Sorry, of course I did wrote something worng.

The row:

has_one :logo, :through => :attachment, :source => :attachable,
:source_type => ‘Maker’, :dependent => :destroy,
:conditions => [‘attachments.content_type = ?’,
‘image/jpeg’ ]

produces the same results of:

has_one :another_logo, :class_name => ‘Attachment’, :foreign_key =>
‘attachable_id’, :conditions => [‘attachable_type = ?’, ‘Maker’ ]

The first row (has_one :logo) doesn’t produce the same result as the
latter at all. It is a “circular”
reference, in the meaning that having:

m = Maker.find(:first)
m.logo returns itself, which is not what I wanted.

What I want is that “m.logo” returns the logo eventually associated with
the object.
So far, the only way I’ve found is to use:

has_one :logo, :class_name => ‘Attachment’, :foreign_key =>
‘attachable_id’, :conditions => [‘attachable_type = ?’, ‘Maker’ ]

Now, the questions are:

  • Is this the best way to accomplish this?
  • Are there any other ways to get the same result?

Thanks again