0

I try to link "Points" to "Words" with a has_and_belongs_to_many relationhip.

I use a table named dictionnaries_points containing "dictionnary_id" and "linked_to" columns.

I specified in the model the name "linked_to" of my column

class Point < ActiveRecord::Base
  has_and_belongs_to_many :words, :association_foreign_key => "linked_to"
end

class Word < ActiveRecord::Base
   has_and_belongs_to_many :points, :foreign_key => "linked_to"
end  

so I don't have any error but :

  • Rails is not giving all the words which match the integer in the "linked_to" column....

  • Rails only make the link with "word_id" and not with "linked_to" in my words table, as the query here under shows :

    irb(main):002:0> p.words

    ?[1m?[36mWord Load (0.0ms)?[0m
    ?[1mSELECT "words".* FROM "words" INNER JOIN "points_words" ON "words"."id" = "points_words"."linked_to" WHERE "points_words". "point_id" = 1?[0m

--> I want ON words.linked_to ... How can I specify that Rails should make the relationship on the "linked_to" column ?

Stéphane V
  • 1,094
  • 2
  • 11
  • 25

1 Answers1

0

create a join table migration points_words with id: false and having both point_id and word_id. If u do, p = Point.find(:all) and call p.words, it will give you all the words that are associated with p.

Hope this helps.

nilay
  • 365
  • 1
  • 10