Profiles being created without foreign keys in related model - ruby-on-rails

I have multiple user types defined in a single user model:
enum role: { staff: 0, clinician: 1, admin: 2 }
Clinician users each have a clinician profile that is automatically created using after_create, and I'm intending to store the clinician_profile id on the users table. For some reason, when the clinician profiles are created, the clinician_profile_id remains null on the users table for all users, including clinician users. How do I fix that?
module ClinicianUser
extend ActiveSupport::Concern
included do
belongs_to :clinician_profile
has_many :lists
has_many :universities, through: :lists
has_many :dispatches
has_many :referral_requests, through: :dispatches
after_create :create_clinician_profile, if: :clinician?
belongs_to :market
validates :market_id, presence: true, if: :clinician?
end
class_methods do
def create_clinician_profile
self.clinician_profile.create!
end
end
end
class ClinicianProfile < ApplicationRecord
has_one :user, -> { where role: :clinician }
end
Users table schema:
create_table "users", force: :cascade do |t|
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.string "encrypted_password", limit: 128
t.string "confirmation_token", limit: 128
t.string "remember_token", limit: 128
t.datetime "confirmed_at"
t.integer "role", default: 0
t.string "first_name"
t.string "last_name"
t.integer "university_id"
t.boolean "approved", default: false
t.integer "market_id"
t.integer "clinician_profile_id"
t.index ["clinician_profile_id"], name: "index_users_on_clinician_profile_id"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["market_id"], name: "index_users_on_market_id"
t.index ["remember_token"], name: "index_users_on_remember_token"

A class method shouldn't be used here to operate on a instance object.
You could use a callback block instead:
after_create do |user|
ClinicianProfile.create(user: user) if clinician?
end
Furthermore the association is defined as a belong_to so the parent object can create the association too but that's just a personal opinion.

Related

Creating a modal instance on the attribute update of another in Rails?

I'm building a Rails app that has modals Outage, Service, Note and User.
Service has a boolean attribute is_down. By default, is_down is false. When the attribute is updated to true meaning the service goes down, an Outage should be created and a Note should also be created with User, automated.
This all happens in one update of the is_down attribute. If Service goes back up, the outage remains intact but now has an end_time.
Here is the 'story line`:
Service model:
class Service < ApplicationRecord
has_many :outages
has_many :notes
# This is where I'm confused
is_down
if self.is_down
Outage.create(start_time: Time.now, reason: nil)
Note.create(user_id: 1, entry: "Outage began at #{Time.now}", service_id: self.id)
end
end
end
Outage model:
class Outage < ApplicationRecord
belongs_to :service
has_many :notes
has_many :users, through: :notes
end
Note modal (a join table between Outage and User)
class Note < ApplicationRecord
belongs_to :outage
belongs_to :user
end
and User model:
class User < ApplicationRecord
has_many :notes
has_many :outages, through: :notes
end
Outage is more like a story line where during the outage, users can enter notes about what they've learned.
Here is the schema:
enable_extension "plpgsql"
create_table "notes", force: :cascade do |t|
t.text "entry"
t.boolean "is_public", default: true
t.bigint "outage_id"
t.bigint "user_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["outage_id"], name: "index_notes_on_outage_id"
t.index ["user_id"], name: "index_notes_on_user_id"
end
create_table "outages", force: :cascade do |t|
t.datetime "start_time"
t.datetime "end_time"
t.text "reason"
t.boolean "is_recurring", default: false
t.string "frequency", default: "None"
t.bigint "service_id"
t.index ["service_id"], name: "index_outages_on_service_id"
end
create_table "services", force: :cascade do |t|
t.string "name"
t.boolean "is_down", default: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
create_table "users", force: :cascade do |t|
t.string "username"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
add_foreign_key "notes", "outages"
add_foreign_key "notes", "users"
add_foreign_key "outages", "services"
end
Besides the initial question of automated creation on update of Service attribute, is_down, is this also a good way to go about implementing this?
I would suggest looking into the lifecycle callbacks for ActiveRecord. You can add an after_save callback to your Service class that checks to see if is_down has changed and then create or close an Outage
class Service < ApplicationRecord
has_many :outages
...
after_save :create_or_update_outage, if: is_down_changed?
...
private
def create_or_update_outage
if is_down
outages.create
else
outages.where(end_time: nil).last.update(end_time: Time.now)
end
end

relational models not working as expected

I created the following Active Record Schema using migrations but the relationships don't correspond to the schema. I've tried resetting, dropping, creating and migrating but in Rails C if i create a User u.User.create!(...), and then query u.groups or u.genres I get 'undefined method'
Thanks for your help
ActiveRecord::Schema.define(version: 20180603211047) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "genres", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "tag"
t.bigint "user_id"
t.index ["user_id"], name: "index_genres_on_user_id"
end
create_table "genres_users", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "genre_id", null: false
end
create_table "groups", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.bigint "user_id"
t.index ["user_id"], name: "index_groups_on_user_id"
end
create_table "groups_users", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "group_id", null: false
end
create_table "playlists", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "link"
t.text "description"
t.bigint "group_id"
t.index ["group_id"], name: "index_playlists_on_group_id"
end
create_table "users", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "name"
t.string "token"
t.date "birthday"
t.string "link"
t.string "playlistId"
t.string "country"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "genres", "users"
add_foreign_key "groups", "users"
add_foreign_key "playlists", "groups"
end
here are the models:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
#before_action :authenticate_user!
has_and_belongs_to_many :genres, :through => :genres_users
has_and_belongs_to_many :groups, :through => :groups_users
include Enumerable
end
class Genre < ApplicationRecord
has_and_belongs_to_many :users, :through => :genres_users
end
class Group < ApplicationRecord
has_and_belongs_to_many :users, :through => :groups_users
has_one :playlist
end
class Playlist < ApplicationRecord
belongs_to :group
end
The relationship is that Groups have users, users have genres (favourite genres!), these are has and belongs to relationships through join tables (multiple genres per user and multiple groups per user). Every group has a playlist, and there will be multiple playlists
[Edited after clarification from OP]
The relationship is that Groups have users, users have genres (favourite genres!), these are has and belongs to relationships through join tables (multiple genres per user and multiple groups per user). Every group has a playlist, and there will be multiple playlists
First off, you don't need a user_id column on groups or genres as that's not how the setup should work.
class Genre < ApplicationRecord
has_many :favorite_genres
has_many :users, through: :favorite_genres
[... other stuff]
end
class User < ApplicationRecord
has_many :group_memberships
has_many :groups, through: :group_memberships
has_many :favorite_genres
has_many :users, through: :favorite_genres
[... other stuff]
end
class Group < ApplicationRecord
has_many :group_memberships
has_many :users, through: :group_memberships
has_many :playlists
[... other stuff]
end
class Playlist < ApplicationRecord
belongs_to :group
end
class GroupMemberships < ApplicationRecord
belongs_to :user
belongs_to :group
[... other stuff]
end
class FavoriteGenres < ApplicationRecord
belongs_to :user
belongs_to :genre
[... other stuff]
end
So you'd drop the user_id column in groups. The connection happens in :group_memberships (the table formerly known as users_groups), which is a user_id, a group_id, and then you can have additional metadata columns as you need them (e.g. admin boolean/role, etc)
. This is called a "Has Many Through" relationship (http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association)
Likewise, a user's favorite genres is setup with a through relationship. So you'll have a separate database table AND model file for those through joins.
I don't think you need your add_foreign_key calls at all at this level, nor many of your indexes. You'll probably do more eager loading or possibly add indexes on the thorugh join tables and you'd do those like this in the schema:
t.index ["user_id", "genre_id"], name: "index_favorite_genres_on_user_id_and_genre_id"
Remember that belongs_to now creates a validation for that to be present in 5.x. You can override this by adding optional: true on that line in the model, e.g. belongs_to :foo, optional: true
So all that being said, here's your new schema:
create_table "genres", id: :serial, force: :cascade do |t|
t.string "tag"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "groups", id: :serial, force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "favorite_genres", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "genre_id", null: false
end
create_table "groups_memberships", id: false, force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "group_id", null: false
end
create_table "playlists", id: :serial, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "link"
t.text "description"
t.bigint "group_id"
t.index ["group_id"], name: "index_playlists_on_group_id"
end
create_table "users", id: :serial, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "name"
t.string "token"
t.date "birthday"
t.string "link"
t.string "playlistId"
t.string "country"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
Give that a whirl (I haven't built this in an app, so there may be some errors in the code) and you should now be able to do your console run:
u = User.create([values])
u.genres (should return nil until you create some relationships)
etc.

has_many through association dependent error is still referenced in table users

Before I start, I should say I have already checked out Rails 5.1.: destroy records in "has_many: through" association with restriction and has_many through association dependent destroy under condition of who called destroy without results.
My app consist in USERS that has_one EMPRESA.
An EMPRESA may have several TAGS
A TAG may have several EMPRESAS (To do this I have used has_many :through)
My case: I got this screen error:
And I know the origin of this error is because I'm trying to destroy items with pending references. But I can't identify th issue.
By looking at server console I can guess the problem involve empresa, tag, and tagging.
Models involved
class Empresa < ApplicationRecord
skip_callback :validate, after: :create
after_initialize :set_default_plan, :if => :new_record?
attr_accessor :tag_list
enum plan: [:noplan, :basic, :plus, :premium]
belongs_to :user
belongs_to :category, optional: true
has_many :promos, dependent: :destroy
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
mount_uploader :logo, LogoUploader
mount_uploaders :fotos, FotosUploader
def tag_list
tags.join(", ")
end
def tag_list=(names)
tag_names = names.split(",").collect {|str| str.strip.downcase}.uniq
new_or_existing_tags = tag_names.collect {|tag_name| Tag.find_or_create_by(name: tag_name)}
self.tags = new_or_existing_tags
end
def set_default_plan
self.plan ||= :noplan
end
end
class Tag < ApplicationRecord
has_many :empresas, through: :taggings
has_many :taggings, dependent: :destroy
def to_s
name
end
end
class Tagging < ApplicationRecord
belongs_to :empresa
belongs_to :tag
end
class Category < ApplicationRecord
validates :name, presence: true, length:{ minimum: 3 }, uniqueness: true
has_many :empresas, dependent: :nullify
end
class User < ApplicationRecord
enum role: [:user, :editor, :admin, :superadmin]
after_initialize :set_default_role, :if => :new_record?
has_one :empresa, dependent: :destroy
has_many :incidents, dependent: :destroy
has_many :comments, dependent: :destroy
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
def set_default_role
self.role ||= :user
end
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
schema.rb (irrelevant tables removed)
ActiveRecord::Schema.define(version: 20180531033550) do
enable_extension "plpgsql"
create_table "empresas", force: :cascade do |t|
t.string "logo"
t.string "name"
t.text "description"
t.text "excerpt"
t.string "address"
t.string "web"
t.string "email"
t.string "tel"
t.string "video"
t.json "fotos"
t.integer "plan", default: 0
t.float "mlon"
t.float "mlat"
t.string "schedule0"
t.string "schedule1"
t.string "schedule2"
t.string "schedule3"
t.string "schedule4"
t.string "schedule5"
t.string "schedule6"
t.string "schedule7"
t.string "schedule8"
t.string "schedule9"
t.string "schedule10"
t.string "schedule11"
t.string "schedule12"
t.string "schedule13"
t.string "schedule14"
t.string "schedule15"
t.string "schedule16"
t.string "schedule17"
t.string "schedule18"
t.string "schedule19"
t.string "schedule20"
t.string "schedule21"
t.string "schedule22"
t.string "schedule23"
t.string "schedule24"
t.string "schedule25"
t.string "schedule26"
t.string "schedule27"
t.integer "tag_id"
t.integer "offer_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.index ["category_id"], name: "index_empresas_on_category_id", using: :btree
end
create_table "taggings", force: :cascade do |t|
t.integer "empresa_id"
t.integer "tag_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["empresa_id"], name: "index_taggings_on_empresa_id", using: :btree
t.index ["tag_id"], name: "index_taggings_on_tag_id", using: :btree
end
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.integer "creditos", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "empresa_id"
t.integer "role"
t.string "first_name"
t.string "last_name"
t.date "birthdate"
t.string "dni"
t.string "phone"
t.string "address"
t.string "gender"
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["empresa_id"], name: "index_users_on_empresa_id", using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
add_foreign_key "comments", "incidents"
add_foreign_key "comments", "users"
add_foreign_key "incidents", "users"
add_foreign_key "promos", "empresas"
add_foreign_key "taggings", "empresas"
add_foreign_key "taggings", "tags"
add_foreign_key "users", "empresas"
end
You do not need empresa_id in users.
Remove the referential integrity and column via:
rails g migration remove_constraint_from_users
Edit the newly created migration file to add the following in def change block
def change
remove_foreign_key :users, :empresas
remove_column :users, :empresa_id
end

Ruby on Rails 4.2, stack level too deep, associations and delegate()

I understand the cause of a Stack Level Too Deep error. I am failing to spot where/why it is occurring in my code base.
I've implemented a multi-model, multi-step wizard. The first two models (User and Company) are working, it is when I attempt to add in the third (Address) I get the error.
I suspect the error is related to the associations between the models, although I've failed to debug.
The code snippets below function correctly except when I add the 3 lines (marked with comments in the snippet) too the file app/wizards/user_wizard/step1.rb.
Relevant Models
app/models/company.rb
class Company < ActiveRecord::Base
include Validatable::Company
# Associations:
has_many :addresses, inverse_of: :company
accepts_nested_attributes_for :addresses, reject_if: :all_blank
has_many :employees, inverse_of: :company
accepts_nested_attributes_for :employees, reject_if: :all_blank
has_many :licenses, inverse_of: :company
accepts_nested_attributes_for :licenses, reject_if: :all_blank
has_many :vehicles, inverse_of: :company
accepts_nested_attributes_for :vehicles, reject_if: :all_blank
has_one :user, inverse_of: :company
end
app/models/address.rb
class Address < ActiveRecord::Base
# Associations:
belongs_to :company, inverse_of: :addresses
has_many :licenses, inverse_of: :address
accepts_nested_attributes_for :licenses, reject_if: :all_blank
has_many :initial_analyses, inverse_of: :address
accepts_nested_attributes_for :initial_analyses, reject_if: :all_blank
end
app/models/user.rb
class User < ActiveRecord::Base
include SoftDeletable
include Validatable::User
# Constants:
MARKER_ATTRIBUTES = %w[user_name].freeze # get marked with '(deleted)'
DEPENDANT_CHILDREN = %w[none].freeze # child resources to be deleted
# Associations:
belongs_to :role, inverse_of: :users
belongs_to :company, inverse_of: :user
accepts_nested_attributes_for :company, reject_if: :all_blank
has_many :auto_quotes, inverse_of: :user
end
db/schema.rb
ActiveRecord::Schema.define(version: 20170616131833) do
create_table "addresses", force: :cascade do |t|
t.integer "company_id"
t.text "site_name"
t.string "premises_code"
t.string "exempt_premises_code"
t.text "address"
t.string "city"
t.string "county"
t.string "sic_code"
t.string "postcode"
t.string "country"
t.boolean "sic_update"
t.boolean "deleted", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
end
create_table "companies", force: :cascade do |t|
t.string "company_name"
t.string "registration_number"
t.string "type_of_business"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "deleted_at"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", default: 0, null: false
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "user_name"
t.datetime "deleted_at"
t.integer "role_id"
t.integer "company_id"
t.string "invitation_token"
t.datetime "invitation_created_at"
t.datetime "invitation_sent_at"
t.datetime "invitation_accepted_at"
t.integer "invitation_limit"
t.integer "invited_by_id"
t.string "invited_by_type"
t.integer "invitations_count", default: 0
end
add_index "users", ["company_id"], name: "index_users_on_company_id", unique: true
add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["invitation_token"], name: "index_users_on_invitation_token", unique: true
add_index "users", ["invitations_count"], name: "index_users_on_invitations_count"
add_index "users", ["invited_by_id"], name: "index_users_on_invited_by_id"
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
add_index "users", ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end
Wizards
app/wizards/user_wizard/base.rb
module UserWizard
class Base
include ActiveModel::Model
STEPS = %w[step1 step2].freeze
attr_accessor :user
delegate(*::User.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :user)
def initialize(user_attributes)
#user = ::User.new(user_attributes)
end
end
end
app/wizards/user_wizard/step1.rb
module UserWizard
class Step1 < UserWizard::Base
include Validatable::Company
attr_accessor :company
# One of 3 lines triggering circular reference by adding in Address model
attr_accessor :address
delegate(*::Company.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :company)
# One of 3 lines triggering circular reference by adding in Address model
delegate(*::Address.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :address)
def initialize(user_attributes)
super
#company = #user.build_company
# One of 3 lines triggering circular reference by adding in Address model
#address = #user.company.addresses.build
end
end
end
app/wizards/user_wizard/step2.rb
ommitted as it is irrelevant. code fails before ever instantiating this class
address has an address attribute. So the delegate method is trying to create a method address that will be delegated to address.
I'd suggest this:
module UserWizard
class Step1 < UserWizard::Base
include Validatable::Company
attr_accessor :company
# One of 3 lines triggering circular reference by adding in Address model
attr_accessor :company_address
delegate(*::Company.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :company)
# One of 3 lines triggering circular reference by adding in Address model
delegate(*::Address.attribute_names.map {|attr| [attr, "#{attr}="] }.flatten, to: :company_address)
def initialize(user_attributes)
super
#company = #user.build_company
# One of 3 lines triggering circular reference by adding in Address model
#company_address = #user.company.addresses.build
end
end
end

Rails 5: find all Users who belong to Companies, which belong to current_user

In my app User can have many Companies and vice versa. In Accounts table id of User and id of its Company is stored.
I want to find all Users who belong to Companies, which belong to current_user. Let's assume that the current_user is like master User (not Admin, as that would be system Admin) of those companies.
How do I do this? My guess is to do it with Arel, but then how should it look in Model, Controller, View? Many thanks for any help. I'm on Rails 5.
models/user.rb
class User < ApplicationRecord
has_many :accounts, dependent: :destroy
has_many :companies, through: :accounts
models/account.rb
class Account < ApplicationRecord
belongs_to :company
belongs_to :user
accepts_nested_attributes_for :company, :user
models/company.rb
class Company < ApplicationRecord
has_many :accounts, dependent: :destroy
has_many :users, through: :accounts
accepts_nested_attributes_for :accounts, :users
My schema.rb looks like this:
create_table "accounts", force: :cascade do |t|
t.integer "company_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["company_id"], name: "index_accounts_on_company_id"
t.index ["user_id"], name: "index_accounts_on_user_id"
end
create_table "companies", force: :cascade do |t|
t.string "name"
t.string "legal_name"
t.string "reg_number"
t.string "address"
t.string "bank_acc"
t.string "description"
t.string "website"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role", default: 0
t.integer "currency", default: 0
end
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.index ["email"], name: "index_users_on_email", unique: true
end
You can find current user's companies, and eager load users who belong to those companies
#companies = current_user.companies.includes(:users)
To list all users(may be in a view), loop through #companies and all its users
#companies.each do |company|
company.users.each do |user|
puts user.name
end
end
Or use map/collect to assign them to a variable.
#users = #companies.map(&:users).flatten

Resources