Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upBug with namespaced ActiveRecord models on 4.1.x (PostgreSQL) #15811
Comments
|
Which version are you using? Have you tried the latest 4.1.2.rc2? Could you create an reproduction script with this template https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_master.rb? |
|
I think it is problem and should be fixed I create an reproduction script here |
|
I find When Post::Special.first.comments is called, comments method is called and Post::Special make a relationship. To make a relationship, Rails use class_name method in this url. so We couldn't cope with using namespaces models at this moment because using has_many method. But you could use namespaces model as follow
|
|
@rafaelfranca Yes, this is with 4.1.2.rc2. I guess I should be specifying the class_name option so not sure this is actually a bug. |
|
Even on 4.1.2.rc3? |
|
So I tried to debug and fix this issue but I think it is not possible. There is no way of the automatic detection of the class name work correctly on this case. When trying to find possible candidates for the class of your association called Only the module I think we could improve the error checking if the candidate is a Active Record model and if not we raise an ArgumentError with the proper information. Something like: "Rails could not find a valid model for @thelucid if we agree with this solution @mdepolli will work on it. |
|
@rafaelfranca Sounds fine to be, using class_name is fine it's just that the error message was a bit cryptic. |
|
+1 Still happening to me on 4.1.5 |
|
I'm using |
|
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
|
To be more precise for others who try @frenkel 's method... I found that the class name has to specifically match the model that contains So in this case you'd write it like this:
Not sure if he's trying to accomplish something different from me, but this makes namespaced models work with polymorphism work for me in rails 3.1. |
|
@matthewd I will work on this. |
|
@shrirambalakrishnan are you working on this? If not can I start working on it? Thanks |
|
@tiagonbotelho I am not working on this. You can start with this. |
|
@rafaelfranca could you point me to the code snippet where:
happens? I also assume that checking if the candidate is an Active Record model is just It is my first time contributing so sorry in advance if they are bad questions Thanks! |
|
Ok so I was able to track down where we pick the candidates (https://github.com/rails/rails/blob/master/activerecord/lib/active_record/inheritance.rb#L143) however I am not being able to find where we actually check if a candidate is a Active Record model |
|
@tiagonbotelho I don't believe there's currently a check - that's the cause of the original error report; the search finds The check is going to be tricky; some libraries ( |
|
Thanks @al2o3cr I created a comment https://github.com/rails/rails/pull/30778/files#r143513045 where I propose a possible solution to the "Checking if the object is a active record model" Also could you provide me the example you mentioned from |
|
@rafaelfranca I've provided my proposed fix in #31829. |
|
Is this still open? Can I pick it up? |
@vedant1811 hello! there are two PRs for this issue, one incomplete and another possibly/likely complete (mine). since the feedback cycle died on mine, I've closed it, but feel free to rip it off and improve it as you please; it's for the community's sake, anyway. |
|
Is this still an issue with master? We wouldn’t accept a backport for Rails 4.1 at this time. |
|
I updated @taish's reproduction script and looks like it still fails on the latest rails. # frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
end
create_table :comments do |t|
t.integer :outline_id
t.string :outline_type
end
end
module Post
end
module Comment
end
class Post::Base < ActiveRecord::Base
self.table_name = 'posts'
end
class Post::Special < Post::Base
has_many :comments, as: :outline
end
class Comment::Base < ActiveRecord::Base
self.table_name = 'comments'
belongs_to :outline, polymorphic: true
end
class Comment::Special < Comment::Base
end
class BugTest < Minitest::Test
def test_association_stuff
post_special = Post::Special.create
post_special.comments << Comment::Base.create
assert_equal 1, post_special.comments.count
assert_equal 1, Comment::Base.count
assert_equal post_special.id, Comment::Base.first.outline_id
end
endOutput:
|
|
Is this issue up for grabs? I would like to make my first PR to the project |
I've come up against a bug with ActiveRecord when using namespaces models and polymorphic associations, it's a common use-case so should probably be fixed before a 4.2 release.
Error:
Example: