i have a need to attach attribute(s) to a one to many association. in
essence it would be an association to an association. similar to a
polymorphic association but not. i wanted this to be in another table so
it could handle multiple types of association attributes. was thinking
i would need “join table name” as well as a concatented key “123_456”
that represents the 2 real indices. what is a good way to generate the
key from either of the related elements.
It would be easier if you actually describe the problem you are trying
to solve.
Lets use Person has_many Things as an example.
It sounds to me like you need to either put these new attributes in
the things table, or create a new assocication from the new table to
things.
On 12 May 2010 15:14, tom [email protected] wrote:
hi, just included a couple of js files into my application layout and
pastedd the jas files into my appropriate js-folder:
I think that should be src=“/javasc…” The leading / tells it to
look in public, I presume they are in public/javascripts. It is
better to use javascript_include_tag however. Then you can just say
<%= javascript_include_tag “growl/mootools-1.2b2” %>
Colin
hi, just included a couple of js files into my application layout and
pastedd the jas files into my appropriate js-folder:
using webrick (all basic setup) gives me that:
ActionController::RoutingError (No route matches
“/javascripts/growl/growl-compressed.js” with {:method=>:get}):
file permissions are ok. i wonder, coz this doesnt happen to all the
other js files…
any ideas?
thx
Sharagoz wrote:
It would be easier if you actually describe the problem you are trying
to solve.
Lets use Person has_many Things as an example.
It sounds to me like you need to either put these new attributes in
the things table, or create a new assocication from the new table to
things.
i pretty much already described it. if you had a HABTM association
between people and books … i wanted to have attributes attached to each
association. for example: for person 5 and book 4 you would have related
records 1 and 2.
On 12 May 2010 16:15, Gone S. [email protected] wrote:
association. for example: for person 5 and book 4 you would have related
records 1 and 2.
It was not entirely clear, nor is it now, though I think I get an idea
what you mean. In the first post you said it was one to many, now it
is many to many. Assuming many to many
Person
has_many book_associations
has_many books through book_associations
Book
has_many book_associations
has_many people through book_associations
BookAssociation
belongs_to person
belongs_to book
has_many somethings (I don’t know what you want to call this)
Something
belongs_to BookAssociation
Does that help?
Colin
On May 12, 5:15 pm, Gone S. [email protected] wrote:
if you had a HABTM association
HABTM is just a convenient way of creating a many-to-many association
when no additional information needs to be stored on the association.
In this case you do, so set up a proper many-to-many association and
put the additional fields on the model in the middle.
Sharagoz wrote:
In this case you do, so set up a proper many-to-many association and
put the additional fields on the model in the middle.
i get your model in the middle idea. but how will this serve different
many to many associations? seems it would need specific info on the
association it is associated to. i wanted to to be generic.
Gone S. wrote:
i wanted to to be generic.
Then you may want to look at has_many_polymorphs… (I’ve always found
the minimalist HABTM to be insufficient - people always want to know who
related something, and when, so my join tables are always full models of
their own)
If A is related to B, and A is related to C, and X is the thing you want
to relate to an A-B relationship, or an A-C relationship, then perhaps
something like this:
A
has_many ABjoins
has_many Bs through ABjoins
has_many ACjoins
has_many Cs through ACjoins
B
has_many ABjoins
has_many As through ABjoins
AB
belongs_to A
belongs_to B
has_many Xlinks as xlinkable
has_many Xs, through Xlinks
C
has_many ACjoins
has_many As through ACjoins
AC
belongs_to A
belongs_to C
has_many Xlinks as xlinkable
has_many Xs, through Xlinks
X
has_many Xlinks
has_many ABjoins through Xlinks, source ABjoin, Xlinks.xlinkable_type
= ABjoin
has_many ACjoins through Xlinks, source ACjoin, Xlinkx.xlinkable_type
= ACjoin
Xlink
belongs_to X
belongs_to xlinkable, polymorhpic => true
belongs_to ABjoin, class_name ABjoin, foreign_key xlinkable_id
belongs_to ACjoin, class_name ACjoin, foreign_key xlinkable_id
does your solution require a book_association model? i wanted the new
association of associations to be generic and not need a model or a
controller. by generic i mean it should handle other asociations like
dogs_persons autos_persons as well. all i really want to do is be able
to associate records in a table with other association records not
restricted to any model or type. i envision the table as having a field
that knows the name of the association (persons_books) and then a
concatentated key of the 2 associated records (46_100 where 46 is index
of person and 100 is index of book). also there can be more than one of
these records associated with the unique record persons_books:46_100.
Ar Chron wrote:
Then you may want to look at has_many_polymorphs… (I’ve always found
the minimalist HABTM to be insufficient - people always want to know who
related something, and when, so my join tables are always full models of
their own)
right this sounds like what i need to do. so your association tables (AB
and AC) have their own unique ids? Xlink is the polymorphic link from AB
and AC to the X table elements? so the Xlink table will have an
xlinkable type and id along with the X table id? thanks.
i had not thought of a polymorphic association.
works good and does what i needed.
Ar Chron wrote:
Gone S. wrote:
right this sounds like what i need to do. so your association tables (AB
and AC) have their own unique ids? Xlink is the polymorphic link from AB
and AC to the X table elements? so the Xlink table will have an
xlinkable type and id along with the X table id? thanks.Yup.
AB or AC would have:
id
a_id
b_id or c_id
plus other fieldsxlink would have:
id (the xlink record id)
x_id (the id of the related X record)
xlinkable_id (this is the id of the AB/AC join record)
xlinkable_type (this is the class name “AB”/“AC” of the join record)
Gone S. wrote:
right this sounds like what i need to do. so your association tables (AB
and AC) have their own unique ids? Xlink is the polymorphic link from AB
and AC to the X table elements? so the Xlink table will have an
xlinkable type and id along with the X table id? thanks.
Yup.
AB or AC would have:
id
a_id
b_id or c_id
plus other fields
xlink would have:
id (the xlink record id)
x_id (the id of the related X record)
xlinkable_id (this is the id of the AB/AC join record)
xlinkable_type (this is the class name “AB”/“AC” of the join record)