1

In Ruby on Rails Guide, is imageable a model? And does imageable have a database table?

How does imageable_id point to two different id attributes for Employee and Product?

3 Answers 3

4

No, in the page that you linked, imageable represents a polymorphic association to either an Employee or Product. In order for the polymorphic association to work, rails will need to determine the 'type' of object (i.e. either an Employee or a Product) by looking at the 'imageable_type' entry in the db and then the id of the respective object (imageable_id).

For example, an entry within the db of imageable_type => 'Employee' and and imageable_id => '4'. ActiveRecord would figure out that the association is to id 4 in the employees table.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I understand where my misunderstanding came from. I though polymorphic association could be used for cases where Image model belongs to Employee and Product at the same time. Not one at a time.
2

The imageable_type helps rails understand that which class the model belongs to. While generating model if you use imageable:references then rails will automatically generate imageable_id and imageable_type columns in your migration.

Comments

1

Imageable is not a model. It is a placeholder for some model (in this case either Employee or Product).

The way imageable_id can point to different kinds of ids is that Picture also has an imageable_type attribute. For a given instance of Picture, it knows what kind of id imageable_id represents by looking at it's imageable_type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.