-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
has_and_belongs_to_many relationships will not store join table #38
Comments
Some prying about revealed why. I threw a pry in here:
And, ignoring the irrelevant good payloads for Account, heres the interesting iterations: # [1] directory(#<Polo::Collector>) » payload
=> {
:sql => "SHOW TABLES LIKE 'accounts_roles'",
:name => "SCHEMA",
:connection_id => 70096124074740,
:binds => [],
:line => 7,
:filename => "/Users/matt/work/dir.caring.com/bin/rake",
:method => "<top (required)>"
}
# [1] directory(#<Polo::Collector>) » payload
=> {
:sql => "SHOW FULL FIELDS FROM `accounts_roles`",
:name => "SCHEMA",
:connection_id => 70096124074740,
:binds => [],
:line => 7,
:filename => "/Users/matt/work/dir.caring.com/bin/rake",
:method => "<top (required)>"
}
# [1] directory(#<Polo::Collector>) » payload
=> {
:sql => "SELECT `roles`.*, `t0`.`account_id` AS ar_association_key_name FROM `roles` INNER JOIN `accounts_roles` `t0` ON `roles`.`id` = `t0`.`role_id` WHERE `t0`.`account_id` IN (2, 5, 6, ... this is a very long list ... ) AND (`roles`.`deleted_at` IS NULL)",
:name => "SQL",
:connection_id => 70096124074740,
:binds => [],
:line => 7,
:filename => "/Users/matt/work/dir.caring.com/bin/rake",
:method => "<top (required)>"
}
# [1] directory(#<Polo::Collector>) » payload
=> {
:sql => "SELECT `roles`.* FROM `roles` WHERE `roles`.`id` IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25) AND (`roles`.`deleted_at` IS NULL)",
:name => "Role Load",
:connection_id => 70096124074740,
:binds => [],
:line => 7,
:filename => "/Users/matt/work/dir.caring.com/bin/rake",
:method => "<top (required)>"
} The Well, I don't see a solution to this problem. 😅 |
Lastly I'll add the way we have "solved" this problem is to redefine the associations when we scrub to be # lib/tasks/db.rake
namespace :db do
task :scrub_setup do
# Redefine the HABTM for roles so we can use polo.
class AccountsRole < ActiveRecord::Base
belongs_to :account
belongs_to :role
end
class Account < ActiveRecord::Base
has_many :accounts_roles
has_many :roles, through: :accounts_roles
end
class Role < ActiveRecord::Base
has_many :accounts_roles
has_many :accounts, through: :accounts_roles
end
end
end
Rake::Task["db:scrub"].enhance ["db:scrub_setup"] |
hey, @bessey. Great investigative work! |
Title perhaps doesn't explain the problem well, code does:
This will result in the INSERT SQL for
account
androle
being created, but not for the join table. The "solution" appears to be to usehas_many through:
instead, and always have an AR model with an ID column as the join table.I don't have a solution, so I feel the README should at least clarify that HABTM doesn't work right now, and to use
has_many through:
instead.Encountered on Rails 3.2
The text was updated successfully, but these errors were encountered: