Adam Akhtar wrote:
[...]
[...]
>> Use a Pairing class with a polymorphic association.
>
> I thought polymorphic associations are used when you have several
> different models which have the same relationship with anohter one. I.e.
> You want to have comments associated with your models for Photos, Posts
> and Products for example. In this case however only my alphabet model
> has a relationship with the pairings model. So im not sure how i
> implement a polymorphic association here and how that that allow me to
> create only one pairing record to represent ab or ba.
On rereading, I realize I was wrong. I thought you had different
*classes* A-Z, but I realize now that you don't, so that you don't need
a polymorphic association after all.
>
>
> my current setup is
> class Pairing < ActiveRecord::base
> belongs_to :alphabet
> belongs_to :alphabetpartner, :class_name => "Alphabet", :foreign_key =>
> "alphabetpartner_id"
That looks right.
>
> class Alphabet < ActiveRecord::Base
> has_many :pairings
> has_many :alphabetpartners,
>
> but this requires me to create two records in hte pairings table for ab
> and ba.
No it doesn't.
[...]
>> Sort the object IDs in some unambiguous way before creating a pairing.
>
> Can you explain more about what you mean here and are you saying that i
> have to have two pairs in my pairing table for ab and ba?
No, I am saying you would *not* have to have two pairs. Rather, when
you construct a Pairing, pick an unambiguous way of sorting the two
Alphabet objects so that ab and ba will map to the same thing. For
example (untested):
class Pairing < AR::B
before_create do
[...@alphabet, @alphabetpartner] = [...@alphabet,
@alphabetpartner].sort_by {|x| [x.name, x.id]}
end
end
And BTW, @alphabetpartner should be @alphabet_partner.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---