Association between category,subcategory and gig

Mike McCallen

My website has Category->Subcategory->Gigs (by gigs i mean lots of adds)

Below are the relationships between the 3 models.

class Category < ActiveRecord::Base
  has_many :subcategories
end


class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :gigs
end


class Gig < ActiveRecord::Base
  belongs_to :user
  belongs_to :subcategory
end

Question:

What rails migrations should i use to make the required relationship between them in schema.rb,for example ("should i create category_id,or subcategory_id,or gig_id...),if yes in what table should they go,i got quite confused about it.

Thank you for your time.

Snags88

A "belongs_to" relationship always requires a foreign key on that table.

With that being said, your gigs table should have

t.integer :user_id
t.integer :subcategory_id

and your subcategories table should have

t.integer :category_id

hope that helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails: Association between Category, Subcategory, and Lawyer

From Dev

Association between Classes and Interfaces

From Dev

association between two models

From Dev

association between three models

From Dev

Showing Category And Subcategory In Laravel

From Dev

Fetch subcategory in fetched category

From Dev

Naming Category and SubCategory Tables

From Dev

how to Laravel Category - Subcategory

From Dev

Laravel category - subcategory

From Dev

Missing ecto association between models

From Dev

Switch direction of association between objects

From Dev

The difference between Aggregation and Association in implementation

From Dev

Association between two models not working

From Dev

Association between products or cross sell %

From Dev

Association between two STI/Polymorphic

From Dev

wordpress show category and subcategory in menu

From Dev

django group subcategory with category slug

From Dev

Display subcategory in main category in laravel

From Dev

Category and subcategory in same or different table?

From Dev

Category and Subcategory display (Parent/Child)

From Dev

Display Category and Subcategory In nested format

From Dev

Subcategory before parent category in Wordpress

From Dev

Restricting posts to a specifc category and subcategory

From Dev

Select category: Rails 4 many to many association

From Dev

Creating an association mapping between two vectors in MATLAB

From Dev

Factory girl conflict between field names with association

From Dev

association and migration between users and teams (rails)

From Dev

Unable to determine the principal end of an association between the types

From Dev

Is this Rails association between Company and Employee valid?

Related Related

HotTag

Archive