What does "imageable" mean in the rails docs explaining polymorphic associations? - ruby-on-rails

So, I am trying to implement a polymorphic association and reviewing the Rails Docs, but am a but confused how they are using "imageable." Is "imageable" some sort of convention or should it really be tied to a table called "imageable"?

imageable is an unwritten naming-convention in Rails polymorphic associations.
Another example is taggable - which refers to an object that can be tagged ie, that is taggable. Or for the imageable example, the object that has an attached image is imageable because it can accept an image.
In this case, you'd have columns on the belongs_to object (Picture) called imageable_id and imageable_type storing the id and model-class of the associated object. You don't need an imageable table... just the columns on the Picture model

Related

Ruby: inheritance or something else

I've a quick and quite basic question to ask.
I would like to create a new model which has a parameter that can be one of several model types.
Ex: the param 'targeted_object' can be either an instance of Model A or an instance of Model B.
For the moment I don't think I need a similar behavior for Model A and Model B, so my first guess is to create a Master model for Model A and Model B named TargetableObject: create inheritance.
But is it the best way to do this or I need to make something else regarding that I presume for now no related behavior for Master object children?
Thanks
If I understand correctly, Polymorphic associations could be what you need.
From the rails guides:
With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model.

Correct naming for join model in Rails

I have two models:
BigHouse
and
User
I have a join table between BigHouse and User that holds a many-to-many relation.
This table is called big_houses_users - I want to know what is the proper way of: naming the model file as well as the class.
If you are using has_and_belongs_to_many, your join table should be called big_houses_users, however you do not need to create a class in your app/models.
The preferred approach is to use has_many :through. For this I don't believe their is a convention to naming the class. I usually go with a relationship that makes sense. In your case this could be something like Residency

Rails polymorphic naming convention

I'm trying to figure out what to name a polymorphic association and not coming up with good options. The class that will contain the polymorphic association is called LicenseBatch, and if it weren't polymorphic, the foreign key would just be owner_id. It seems that the go-to polymorphic name would be ownerable, but what if I have another class that is ownable? I'd like this to be more specific, but things like batch_ownerable sound awkward.
Any suggestions or similar situations you have seen before?
Try to put a name that automatically refers to the model name, so if the model is Comment I would use commentable_type and commentable_id. For this specific case I would use:
licensable_batch_type and licensable_batch_id

Rails determine if association is has_one or has_many

Wondering if there is an easy way to determine dynamically if a model's association is a "has_one" or "has_many" relationship (i.e. is this an association to one object or many).
I'm using MongoMapper, so I am able to check if a class klass has an associated model assoc with a one or many relationship via
klass.associations[:assoc].is_a? MongoMapper::Plugins::Associations::OneAssociation
klass.associations[:assoc].is_a? MongoMapper::Plugins::Associations::ManyAssociation
but this seems rather clunky, and isn't generic (i.e. won't work for ActiveRecord associations as well). I'd also like to avoid loading any objects, so I'm pretty sure that instance.assoc.is_a? Array is out too.
Any ideas?
UPDATE: So, I happened upon the Reflections Class methods http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html
You can get all the has_many, belongs_to, etc. with reflect_on_all_associations method. It's all in there. Or you can put in an association into reflect_on_association and it will tell you if it is a has_many, has_one, etc. Specifically:
Model.reflect_on_association(:assoc).macro
This should be sufficient for all cases. It doesn't actually solve the problem I'm currently working on, but it should solve this.

set up a table thing_id field that choose to have relationship with tables base on thing_type

lets say I have a table call payment that have a field call thing_id that will be the foreign key of buy and storage depend on the thing_type field in payment table.
How do I implement this relationship? In migration file , how do I define the foreign key?
In model file , I should use :through ?
I am not sure, but I think you are talking about Polymorphic associations in Rails.
Have a look here: Railscasts - Polymorphic Association

Resources