This project is archived and is in readonly mode.
ActiveRecord has_and_belongs_to_many doesn't include self.table_name_prefix
Reported by Juan77 | August 13th, 2010 @ 07:11 PM
I have downloaded Rails 3 --PRE tuesday, and I have created 2 models with these syntaxs:
r g model system/user
r g model system/group
Now both are created in models/system with System::User and System::Group and added has_and_belongs_to_many :groups and has_and_belongs_to_many :users respectively.
Now I have added one user and one group in the console succesfully, both tables are in "t_sys_users" and "t_sys_groups", since I setup self.table_name_prefix as follow:
def self.table_name_prefix
't_sys_' end
But when I try to retrieve from console g.users or u.groups, hte following errors message show in the console:
Mysql::Error: Table 'business_dev.groups_users' doesn't
exist
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/mysql_adapter.rb:284:in `query'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/mysql_adapter.rb:284:in `execute'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0.rc/lib/active_record/connection_adapters/mysql_adapter.rb:435:in `columns'
It's seems clearly that rails have don't added the name prefix to the table "groups_users", that should be "t_sys_groups_users".
Sincerely.
juan77.
Comments and changes to this ticket
-
Neeraj Singh August 13th, 2010 @ 09:08 PM
- Importance changed from to Low
Can you show your schema.rb and your model in complete so that I could try your case.
-
Juan77 August 13th, 2010 @ 09:47 PM
Hi Neeraj,
Thanks for your response, here is the extract from schema.rb related to user and group, note that schema.rb was built with rake db:schema:dump.
create_table "t_sys_groups", :force => true do |t|
t.string "name", :limit => 50, :null => false t.string "description", :limit => 250 t.string "sysCreatedBy", :limit => 20, :null => false t.datetime "sysDateCreated", :null => false t.string "sysModifiedBy", :limit => 20 t.datetime "sysDateModified" t.integer "sysVersion"
end
add_index "t_sys_groups", ["name"], :name => "xname", :unique => true
create_table "t_sys_groups_users", :id => false, :force => true do |t|
t.integer "group_id", :null => false t.integer "user_id", :null => false
end
add_index "t_sys_groups_users", ["group_id", "user_id"], :name => "groupId", :unique => true add_index "t_sys_groups_users", ["group_id"], :name => "FKC3322168779D6A1C" add_index "t_sys_groups_users", ["user_id"], :name => "FKC33221685DE005CE"
create_table "t_sys_users", :force => true do |t|
t.string "name", :limit => 50, :null => false t.string "password", :limit => 20, :null => false t.date "startDate" t.boolean "active", :null => false t.string "sysCreatedBy", :limit => 20, :null => false t.datetime "sysDateCreated", :null => false t.string "sysModifiedBy", :limit => 20 t.datetime "sysDateModified" t.integer "sysVersion"
end
add_index "t_sys_users", ["name"], :name => "xname", :unique => true
Then the models are:
User (located in models/system/user):
class System::User < ActiveRecord::Base
has_and_belongs_to_many :groups validates_presence_of :name, :password, :active
end
Group (located in models/system/group):
class System::Group < ActiveRecord::Base
has_and_belongs_to_many :users validates_presence_of :name
end
And Finally system.rb (located in models):
module System
def self.table_name_prefix 't_sys_' end
end
Hope these code will helps...
Thanks,
juan77.- -
Samuel Kadolph August 14th, 2010 @ 04:03 AM
Currently habtm doesn't take into account the namespaces for computing the join table like the other associations.
See https://rails.lighthouseapp.com/projects/8994/tickets/4975-model-na... for related which would suggest the code has to be changed to support namespaces like the other associations.
-
Kane August 14th, 2010 @ 07:36 PM
Currently habtm doesn't take into account the namespaces for computing the join table like the other associations.
Samuel, i told you before, namespaces doent go into the table_name and this is the expected behaviour. As rdoc says:
Nested classes are given table names prefixed by the singular form of the parent‘s table name. Enclosing modules are not considered.
@juan this is no bug the join_table for habtm does not respect the tablename prefix cause habtm does not use the tablenames of the joined classes.
as described in rdoc:
Unless the join table is explicitly specified as an option, it is guessed using the lexical order of the class names.
if you want your jointable other than this you have to specifiy it.
I understand that you and Samuel arent happy with this, and really neither am i.
i had a discussion about namespaces going into the tablename with koz some time ago. i think this matter should be addressed in a different way. maybe a discussion about this in the mailinglist.
Discussing this in a kinda bugreport like ticket with a constricted view on the issue only addressing habtm, will not help much.I could imagine modifing habtm to use the table_names and not the class_names would be better and more like what the user expects. This could be handled without talking about namespaces at all, but even there we need a discussion if the two tablenames should be simply concated or if there should be some mechanism so the prefix is only used once and so on.
-
Juan77 August 15th, 2010 @ 02:38 PM
Hi Samuel & Kane,
Now understand a bit more the discussions you have, I respect your opinions and now use :join_table to acomplish my requirements and works great. My humildy opinion is that if these two models belongs to the same namespace I expected as a common behaviour to include the same namespace prefix. For me if one person uses namespaces for models I interpret that all the models belogns usually to at least one namespace, and if two modules has different namespace would be "my_model_with_namespace_has_the_other_model_with_namespace."
So my idea is if I have four models like: admin/user, admin/group, hr/role and system I expect:
admin_groups_has_users (because belogns to same namespace, and groups frist because of letter g)
admin_groups_has_hr_roles (different namespaces)
admin_users_has_hr_roles (start admin, because a is first than h).
systems_has_admin_groups (system hasn't namespaces, so be first).Of course we can remove 'has", but I think that helps to be more understandable, and if the users don't like anything of these they have always :join_table.
These are some ideas you I can contribute to the debacle :P
thanks,
juan77 -
Andrea Campi October 17th, 2010 @ 02:23 PM
- Tag changed from macos 10.6, activerecord-store, rails3.0rc to activerecord-store, rails3
-1 I agree with Kane, if we want to address this, it needs to be comprehensive. #4975 looks more promising to me.
-
Santiago Pastorino February 2nd, 2011 @ 04:28 PM
- State changed from new to open
This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.
Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.
-
Santiago Pastorino February 2nd, 2011 @ 04:28 PM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>