Skip to content
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

Symbol-type class_names in associations doesn't work #1363

Closed
VorontsovIE opened this issue May 27, 2011 · 9 comments
Closed

Symbol-type class_names in associations doesn't work #1363

VorontsovIE opened this issue May 27, 2011 · 9 comments
Assignees

Comments

@VorontsovIE
Copy link
Contributor

I've two models:

class User < ActiveRecord::Base
end

class Deal < ActiveRecord::Base
  belongs_to :owner, :class_name = >:User # or :user
end

Such code in controller:

@deal.owner = current_user

causes an error message: undefined methodmatch' for :user:Symbol but simplу changing:class_name => 'User'` solves the issue. I think it's Rails-style to make both string and symbolic types acceptable.

@sikachu
Copy link
Member

sikachu commented Jun 1, 2011

This is a long running issue. I can tell that it exists since 2.x.x. I'll have a look into it.

@ghost ghost assigned sikachu Jun 1, 2011
@josevalim
Copy link
Contributor

"I think it's Rails-style to make both string and symbolic types acceptable." No, it is not. This is common on some hashes due to security reasons, but it is not a general style.

@yfeldblum
Copy link

@josevalim what is the general style regarding hash keys?

@alexeymuranov
Copy link
Contributor

I haven't noticed this and reported a similar issue (#2530) again.
In my opinion, passing class names as Symbols is more natural, than as Strings.
After all, it is not the content of the "ClassName" that matters here, even if it needs to be parsed.

At least it needs to be documented somewhere.

@yfeldblum
Copy link

It is more natural to pass class names as strings, especially when the class name is namespaced.

belongs_to :owner, :class_name => "Admin::User"

@alexeymuranov
Copy link
Contributor

@yfeldblum why?
Splitting on "::" is the only parsing needed, so is this the only reason to pass class names as strings?
Is there a reason to not implement some time in the future splitting on "::" as Symbol#split?
In my opinion, class names are no less "immutable" than attribute names, and attribute name are usually passed as symbols.

@yfeldblum
Copy link

It is more natural for the programmer to write

belongs_to :owner, :class_name => "Admin::User"

than for him to write

belongs_to :owner, :class_name => "Admin::User".to_sym

or some variant thereof.

@alexeymuranov
Copy link
Contributor

I would write :'Admin::User'.
I meant being natural from the point of view of the language, not typing.
I agree that to some extent this is also a matter of taste.

@mjnissim
Copy link

mjnissim commented Sep 1, 2012

:class_name=>:user should simply look for User in upper case. When needed, strings can be passed, but it is natural to use symbols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants