Correct naming for join model in Rails - ruby-on-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

Related

Why does Rails use the base class name, and not the subclass name, with polymorphic associations and STI?

Say I have Single Table Inheritance with BuyerInvoice inheriting from Invoice.
If I assign such an invoice to a polymorphic association, Rails will store e.g. record_type: "Invoice" rather than record_type: "BuyerInvoice". It stores record.class.base_class.name.
What are some reasons they may have done this? I'm implementing something vaguely similar and would like to understand why Rails might have made that decision.
Best I can think of is that it makes it a bit easier to rename subclasses without affecting associations, though doing it the other way would make it easier to rename abstract superclasses…
STI is an implementation detail of a model, it should not leak into its relations.
Other than renaming subclasses:
STI can be implemented with custom class resolution (with type column not being a class name at all), even have some weird cases where class changes dynamically based on attributes etc.
STI can be added to a model after it already has relations
STI can be removed at all

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.

In Ruby on Rails, does a child class inherit the parent class' associations?

I have an abstract class called Work and another class I called Fanfic. In the Fanfic model, I had that it belongs_to :user and has_many :characters along with other associations. I decided that it would be easier when I add more classes that they will be subclasses of Work instead of having all of the types of works as totally separate classes.
Now I'm wondering if I could write the associations ALL of the works would have in the Work model, with all of the sub-classes keeping all of those associations as well.
Sorry if it's a bit confusing. Let me know if you need clarification or if you need to see my code, or if you need any other information.
Not only should you define associations in Work (super) class (and they will be usable in subclasses), you should also use works table for all the subclasses of Work.
It's typical case of Single Table Inheritance.
To enable it, you just need to add type column to works table (add migration for it).
When you query for Tales, for instance, Rails creates the following SQL query:
SELECT * FROM WORKS WHERE type='Tale';
There is a great (and consice) writeup about this concepts in Rails documentation: http://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html

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

How does the Rails' single table inheritance works?

I have a user table, and a teacher that I newly created. The teacher is sub class of user, so, I use scaffold generator to generate the teacher table, than, I modify the model to do teacher is subclass of user. After all that, I did a db:migrate. Then, I go to
http://localhost:3000/teachers/new
It shows an error:
undefined method `teacherSalary' for #<Teacher:0x103331900>
So, my question is what did I do wrong? I want to create a page for doing user register, the user can ONLY be a teacher / student. But I can't add a teacher record ... ... Moreover, I go to
http://localhost:3000/users/new
I want to have a combo box that allow user register their user to be a "teacher" or a "student". But everything seems not work like I expected. What I need to do? Thank you very very much for your help.
Within your database you should have a single table called users. This table should have a string column which by default is called type. If you use another name for this column then you will have to set the inheritance column name manually using self.inheritance_column = "column_name"
Within your application you have three models, User, Student and Teacher. User inherits from ActiveRecord::Base as usual, Student and Teacher both inherit from User.
You should then be able to instantiate new Teacher and Student objects. Internally this works by writing the model name to the type field on the user tables and then when you use Student.find it adds a clause to the SQL to only return rows where the type = 'Student'
You can add shared behaviour to the User class, e.g. validations etc then add additional behaviour to the inherited classes.
A fuller description of how STI works can be found in Martin Fowlers Book(Patterns of Enterprise Application Architecture).
I found this definition really handy:
STI means one table contains the data of more than one model, usually differentiated by the "type" column. ("users" table contains data for the models "Teacher", ""Pupil", "Employee", "Assistant", etc.)
Keeps similar models in the same table instead of creating new ones.
A Polymorphic Association means that one model can be associated with more than one other model(Comment can belong to post, image, file, user_type...)
To prevent foreign key conflicts, the association is reperesented with the *_id and *_type columns instead of only *_id.
For what you have here , I am not sure if STI is the best way go . STI should generally be used when there is a OO like inheritance and the Models have the same Attribute but different behaviour . In your case Teacher and Student can sure have a few shared attributed , but they are also bound to have different ones as well .
You might want to experiment with a polymorphic association as well .

Resources