Rails 4 Many to Many Association Not Working

Rails 4 - association not working correctly

NameError: uninitialized constant Manager::BlogPost

You have has_namy :blog_posts defined in manager.rb, so Rails will look for a classname BlogPost which isn't exist in your case.

You should explicitly specify the classname to tell Rails to use it.

# app/models/manager.rb
class Manager < ActiveRecord::Base
has_many :blog_posts, class_name: 'Blog::Post'
end

Rails 4 has_many through for many to many association fails when creating association

Swap the positions of permissions and policies in the migration so that you create permissions_policies instead of policies_permissions. Rails infers the class names for a join table in alphabetical order.

See this answer for more information on join table naming conventions.

Rails 4: has many through association error

Rails convention expects your through table to be named: collections_releases. And your model CollectionsRelease. Of course you could override if you wanted to keep the singular collection_releases name.

Has many through association not working in upgrade rails 3 to rails 4 application

Why not just define a method for it rather than a has_many :through?
e.g

def demand_supply_links
DemandSupplyLink.where(supply_type: 'WO',
supply_base_id: base_id,
supply_lot_id: lot_id,
supply_split_id: split_id)
end

This will perform the same operations you are requesting and does not involve raw SQL. It will also handle sanitation much better than string concatenation.

Rails 4 belongs_to/has_many relationship not working

You need to change the article_params to below

def article_params
params.require(:article).permit(:headline, :lede, :body, :type_id, :category_ids => [], :region_ids => [], :story_ids => [])
end

Notice the change :type_id => [] to :type_id

Rails: ActiveRecord has_many association not working

Add

has_many :user_graphs

to the User and Upload classes.

The :through option defines a second association on top of this one.

Many-to-many relationship only working one way in Rails

You use Single Table Inheritance. 3 of your models: DeliveryDirector, AccountDirector and AccountManager are descendants of User model. When doing shallow request it works fine, but when you construct requests which involve all 3 models Rails cannot build the right query. If you try to project how to find all companies of a delivery director in terms of database, you will come to the chain of tables:

companies -> users (account managers) -> pods -> users (account directors) -> users (delivery directors)

The SQL query for your request may look like:

SELECT companies.* FROM companies
INNER JOIN users AS account_managers ON companies.account_manager_id = account_managers.id
INNER JOIN pods ON account_managers.pod_id = pods.id
INNER JOIN users AS account_directors ON pods.account_director_id = account_directors.id
INNER JOIN users AS delivery_directors ON account_directors.delivery_director_id = delivery_directors.id
WHERE delivery_directors.id = 2;

but obviously, Rails does not add AS clause to the query to distinguish user roles and uses users table name instead. To filter results it uses condition "users"."type" IN ('AccountDirector') which is not enough in your case, because in your query there should be also AccountManager (as a link between pods and companies).

Another sign that Rails is confused: despite correct association in your models Rails tries to use table account_directors_companies which you obviously do not have.

I would recommend to review your database schema and extract user roles and relationship between them into separate substances.

UPDATE:

For example, user authentication/registration data can be left in users table as it is now. All info about user roles and their relations can be moved to extra tables, backed up by models:

class DeliveryDirector < ApplicationRecord
belongs_to :user
has_many :account_directors
has_many :pods, through: :account_directors
has_many :account_managers, through: :pods
has_many :companies, through: :account_managers
end

class AccountDirector < ApplicationRecord
belongs_to :user
has_one :pod
has_many :account_managers, through: :pod
has_many :companies, through: :account_managers
end

class AccountManager < ApplicationRecord
belongs_to :user
has_many :companies
end

Each of these models has their own table in the database.

Thus, to fetch companies of delivery director you could call:

DeliveryDirector.find_by(user_id: user_id).companies


Related Topics



Leave a reply



Submit