rubyonrailsin

A Ruby and Rails talk

Sunday, March 28, 2010


[Rails] Polymorphic association and has_one retrieval issues

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

class Address < ActiveRecord::Base
belongs_to :addressable, :polymorphic => true
end

class Client < ActiveRecord::Base
has_one :address, :as => :addressable
end

Now I'am able to create a correct entry in the db for address with
addressable_id = 1#client_id
addreassable_type = 'Client'

but when I retrieve
client = Client.find(1) --> return client instance with id =1
address = client.address -> address is not nil

BUT
address.attributes == nil

Somehow I can't retrieve the address back using association
client.address even though I could create it.
I must be missing something simple. Would appreciate any tips.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive