Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Models with almost same attributes

I'm pretty new to rails and I'm trying to implement these things in my application but I'm not sure how to proceed and what ways is the best.

I would like to have different type of activities like restaurant, gym, store, ecc. All these type of activities have the same 'base' attributes like name, city, address, email, but for example restaurant can also have 'food type' attribute or gym can have 'courses'. I was thinking about not creating different models and so different tables that have redundant attribute, but creating a base model 'activity' and then models for 'store', 'gym', etc..

What is the best solution and how can i achieve it? I looked for STI and multiple inheritance table, but I'm not sure if they are suitable and which solution is the best.

like image 579
Alex Angelini Avatar asked Feb 01 '26 22:02

Alex Angelini


1 Answers

Your question is a bit vague, but it does sound like you'd want to use relations, not inheritance.

A class (or model in the rails MVC paradigm) can have or belong to another model or models. That's called a relation. The models don't inherit from each other (although that is possible for other use cases.) They just have relationships between each other, the same way that database tables can have relationships between them. In fact, relations are how you declare the relationships between the underlying database tables for models.

Address is not the only model that has a relation to gym. A gym is also a business, so it belongs_to the business class. It has one and only one address so it has_one address.

You create models that each have their own attributes (attributes are created with a migration). How you set them up depends on your needs. Since your gym has to have an address, you create an Address model that contains the attributes for an address:

class Address
  # your attributes are in the database as created by your migration(s). You don't need to list them in the class.
  # validations, methods, etc.
end`

You then use your address model for anything that has an address. You don't need to repeat yourself by including address attributes in every class.

Then you set the relation between Gym and Address and Business in the model class.

Since your gym is a Business, (One of the many businesses in your global conglomerate :) you declare that it belongs_to the Business model.

A class can belong to more than one other class. A class can have one of another class or it have have many of another class. (There's also has_and_belongs_to_many, which is a bit more complicated.)

So here's your Gym model:

class Gym

  belongs_to :business

  has_one :address
  has_many :employees
  has_many :members
  #rest of Gym class

end

Now you can reference the the gym's address like this:

gym_one.address.line_1
gym_one.address.line_2
gym_one.address.ship_attention
gym_one.address.city
gym_one.address.state
gym_one.address.zip
gym_one.address.country

You can also reference which one of your businesses the gym belongs to:

gym_one.business

Assuming you set the relation between business and address you can get the address attributes for the gym's parent business:

gym_one.business.address.city

Now that you have a Gym class, you don't have to repeat your attributes in every class. You only add the attributes that are unique to a gym in your migration:

In db/migrations/{date_code}_create_gym:

class Gyms < ActiveRecord::Migration
  def change
    create_table :gyms do |t|
      t.string :name
      # Other attributes that only a gym has. Not attributes of things that have a relation to Gym
    end
  end
end
like image 139
Steven Hirlston Avatar answered Feb 04 '26 13:02

Steven Hirlston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!