-3

I feel like this is a really easy question but i've been stuck for a while.

I have three models, user, listing and admin. Please how do i make both user and listing belong to admin?

  • Have you considered [reading the documentation](http://guides.rubyonrails.org/association_basics.html) or doing some research before asking? – mtkcs Jan 26 '16 at 01:10

2 Answers2

1
class Admin < ActiveRecord::Base
  has_many :users
  has_many :listings
end

class User < ActiveRecord::Base
  belongs_to :admin
end

class Listing < ActiveRecord::Base
  belongs_to :admin
end

class AddAdminUserListing < ActiveRecord::Migration
  def up
    create_table :admins do |a|
      a.string  :name
    end

    create_table :users do |u|
      u.integer :admin_id
    end

    create_table :listings do |l|
      l.integer :admin_id
    end
  end

  def down
    drop_table :listings
    drop_table :users
    drop_table :admins
  end
end

If this is not what you are looking for then please clarify your question.

Information on these sorts of basic questions can be obtained at: http://guides.rubyonrails.org/

James B. Byrne
  • 1,048
  • 12
  • 27
0

class Admin < ActiveRecord::Base

has_many :users
has_many :listings

end

class User < ActiveReecord::Base

belongs_to :admin

end

class Listing < ActiveReecord::Base

belongs_to :admin

end

Lymuel
  • 574
  • 3
  • 10