Has_one association with :conditions: is this somehow possible?

What is the right way to achieve something like this:

class Design < ActiveRecord::Base
belongs_to :card_collection
after_save :generate_images

has_one :background,
:as => :resourceable,
:class_name => “Resource”,
:conditions => [ ‘title = ?’, self.id ],
:dependent => :destroy
end


Everything here is working as expected. ONLY the conditions part is
not working…

The problem is that self.id is NOT the @my_design.id (the id of the
active record design instance), but the object_id of the instance. HOW
can I tell ActiveRecord that I want the @my_design.id ???

Thanks for any help!

Feurio

On 12 Mar 2008, at 19:54, Feurio wrote:

     :conditions => [ 'title = ?', self.id ],

Set conditions to ‘title =#{self.id}’ (make sure it’s single quotes
not double quotes). Activerecord should interpolate it for you.

Fred

Hi Fred,

thanks for the hint. I finally found out how to get it work:


class Design < ActiveRecord::Base
belongs_to :card_collection
after_save :generate_images

has_one :background,
:as => :resourceable,
:class_name => “Resource”,
:conditions => [ ‘title = ?’, ‘#{self.id}’ ],
:dependent => :destroy
end


…by putting it in single quotes it worked.

Thanks.

Feurio

On 12 Mrz., 21:08, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs