A
A
artyomst2010-11-28 18:07:15
Ruby on Rails
artyomst, 2010-11-28 18:07:15

rails. Active Record. Associations?

There is an Images table. How to properly associate Images to itself using migrations? How to correctly name the additional linking table? What to register in the Image model?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
v1z, 2010-11-29
@v1z

Everything, now I understand.
You create the table image_diff (for example).
fields: image_id, second_image_id, percent
Class ImageDiff:
belongs_to :image
belongs_to :second_image, :class_name => "Image", :foreign_key => "second_image_id"
Class Image:
has_one :image_diff # one-to-one relationship because first_image_id binds to the Image model
and it will be possible to access:
im = Image.first
im.image_diff.percent
im.image_diff.second_image # here it will display all the links, so you will need to iterate over them one by one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question