Rails relationship with a relationship - ruby-on-rails

I have a user and a merchant that have a relationship. I need to store information about the relationship, so I am using => through user_merchant_relations.
1) Is my naming convention incorrect? I've read some answers that indicate it might have to be merchant_user
2) Should I force the "join" / "through" table to NOT have a separate ID (e.g. only have the two foreign keys)?
3) In the relation table I reference a program_id. A User-Merchant relation can only have one Program ... but Programs will belong to many User-Merchant relations ... what is the correct way to handle this?
Thank you very much.

1 - The convention is that the models should be ordered alphabetically, so the name should be merchant_user
2 - It seems that with the logic you explained there will be no merchant_user with the same merchant and user at the same time, so you can skip the id. Remember to properly create the validations.
3 - It seems that Program has many MerchantUser and MerchantUser belongs_to Program

Related

Ruby on Rails child with multiple parents options

I encountered against the typical case of a model (Address) and multiple models having this data (Company, Person). Let's work with these models as example, but can be generalized with any others.
Making the correct choice is important, as changing the database schema or making deep changes are not easy later.
Initially it will be one-to-one association, then we have these possibilities:
1) Use the classical normalization and put all the address data into each model requiring it. Then create an address subform for render partial into each one, and put the code behavior into a Module to be called from each model controller using it.
This could looks fine, but it has the problem that is hard to make changes, as any change in the Address data needs to be done on each model using it. Also hard to change it to a one-to-many association, if required.
2) Rails polymorphic feature. Address is a model itself, and belongs_to :addressable, polymorphic: true, then Company and Person have has_one/has_many :address/:addresses, as :addressable. Then add the polymorphic FK to Address table and it's done.
I like this solution, it is clear and with only 2 extra columns we have done it. The only contra I can think about is we rely in a framework feature, so if someday we migrate to another one, it should feature polymorphic too. Well this is not really true because we can always make the SQL manually searching by addressable_type = "type", but I think is not database design standard.
3) Join tables. One for each parent. So we have JT_Company_Address, JT_Person_Address, and we use the Rails has_one/has_many :through associations using those join tables.
I think using join tables is more database design standard, but add some extra tables.
4) Reverse the relationship, making Address the parent and each of the other a child. So each child would have a FK address_id.
I don't like much this one, as I will handle mainly companies and persons, and then look up their address if required, not the opposite, looking addresses and then loop up what it has in it. We can always use the bidiriectional association using the Rails inverse_of, but at database layer Address would be the parent one anycase.
My favorites at this moment are 2 and 3. I will probably use Rails and not move from it, so the polymorphic looks very easy and clean.
Then, what do you think, which one is the best one? Any advice is welcome.
Thanks.

achieve habtm on single model in rails

I have a problem with habtm on a single model in rails.
Example:
Let us say i have a User model with two roles "Student" and "teacher". User model is common for two roles. Now
Each student can be associated to many teachers
Each teacher can be associated to many students
In rails notation, their should be habtm between teacher and student
How this can be achieved with single table.
Thanks,
Aashish
It can't be done with a single table. In a many-to-many relationship, no matter what, you always need a table where you store the associations.
In your case, given the association seems to be parent/child, then you just need two tables instead of one.
How to implement it, it depends on your database structure and data organization. You should create an users_users table (as part of the habtm) and configure the references accordingly. If the user table, as it seems to be, is also used for STI, then the configuration may change a little bit.

RoR: belongs_to_many relationship in Rails?

I have a Model of Key Termsthat can belong to many Articles (1 Term can be used in many articles)? so a user can add key terms used and their explanations to articles for which they are used in? what would be the best way to establish this relationship?
Thank You
You'll want to use a many-to-many relationship so you'll need to use has_and_belongs_to_many (see here). And you'll need to create an interim table for key_term_id and article_id columns named key_terms_articles to maintain the connection.
I think what you want is what's referred to as a HABTM (has and belongs to many) relationship. There's a lot of stuff out there on the topic, and it can be a bit confusing at first to grasp. Try searching for "activerecord HABTM" or "Activerecord polymorph" and that should get you started.

rails 3, multiple database connections and setting up models

When working with multiple database connections and setting up models to work with those different instances, how do you setup the has_many, has_one, belongs_to, etc?
For example:
I have one database that is Read + Write, the other DB instance is used in my Rails app as Read only. The DB table that is Read only I am pulling back a list of Media items (Videos, Images and Audios). In my Read + Write DB I have a media_ratings table.
I have a model called AvMedia (The Read only DB) and a MediaRating Model (Read + Write DB). How do I setup The AvMedia model like so: has_one rating and setup the MediaRating model like so: has_many AvMedia?
Sorry if this is confusing... I tried to explain it the best I could.
In the AvMedia model you must include has_one :rating and just make sure that in the corresponding table you have a rating_id foreign key. You also need belongs_to :avmedia in your rating model.
A lot of magic goes on behind the scenes to automatically link your tables together when you define a relationship.
It sounds like you don't need the has_many for AvMedia - from what you said it appears that it's a 1-1 relationship (one AvMedia has one rating, one rating corresponds with one AvMedia). If this is incorrect, let me know.

Rails ActiveRecord Relationships

How do the relationships magically function when only the models are altered?
If I want a "has__and___belongs___to__many" relationship, what should I name the table (so Rails can use it) that contains the two foreign keys?
Short answer: You can't just tell the models that they're related; there have to be columns in the database for it too.
When you set up related models, Rails assumes you've followed a convention which allows it to find the things you wrote. Here's what happens:
You set up the tables.
Following conventions in Rails, you name the table in a particular, predictable way (a plural noun, e.g. people). In this table, when you have a relationship to another table, you have to create that column and name it in another predictable way (e.g. bank_account_id, if you're relating to the bank_accounts table).
You write a model class inheriting from ActiveRecord::Base
class Person < ActiveRecord::Base
When you instantiate one of these models, the ActiveRecord::Base constructor looks at the name of the class, converts it to lowercase and pluralizes it. Here, by reading Person, it yields people, the name of the table we created earlier. Now ActiveRecord knows where to get all the information about a person, and it can read the SQL output to figure out what the columns are.
You add relationships to the model: has_many, belongs_to or has_one.
When you type something like, has_many :bank_accounts, it assumes a few things:
The name of the model that you relate to is BankAccount (from camel-casing :bank_accounts).
The name of the column in the people table which refers to a bank account is bank_account_id (from singularizing :bank_accounts).
Since the relationship is has_many, ActiveRecord knows to give you methods like john.bank_accounts, using plural names for things.
Putting all of that together, ActiveRecord knows how to make SQL queries that will give you a person's bank accounts. It knows where to find everything, because you followed a naming convention that it understands when you created the table and its colums.
One of the neat things about Ruby is that you can run methods on a whole class, and those methods can add other methods to a class. That's exactly what has_many and friends are doing.
This works because you are following "Convention over Configuration".
If you state that a customer model has many orders then rails expects there to be a customer_id field on the orders table.
If you have followed these conventions then rails will use them and will be able to build the necessary SQL to find all the orders for a given customer.
If you look at the development.log file when you are developing your application you will be able to see the necessary SQL being built to select all orders for a given customer.
Rails does not create tables without you asking it to. The creation of tables is achieved by generating a migration which will create/alter tables for you. The fact that you create a customer model and then state within it that it has_many :orders will not create you an orders table. You will need to do that for yourself within a migration to create an orders table. Within that migration you will need to either add a customer_id column or use the belongs_to: customer statement to get the customer_id field added to the orders table.
The rails guide for this is pretty useful

Resources