This project is archived and is in readonly mode.
ActiveRecord has_and_belongs_to_many doesn't include self.table_name_prefix
-
Neeraj Singh
- Importance changed from to Low
Can you show your schema.rb and your model in complete so that I could try your case.
-
Juan77
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 => falseend
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, :activeend
Group (located in models/system/group):
class System::Group < ActiveRecord::Base
has_and_belongs_to_many :users validates_presence_of :nameend
And Finally system.rb (located in models):
module System
def self.table_name_prefix 't_sys_' endend
Hope these code will helps...
Thanks,
juan77.- -
Samuel Kadolph
Currently habtm doesn't take into account the namespaces for computing the join table like the other associations.
See #4975 Model Namespaces and HABTM for related which would suggest the code has to be changed to support namespaces like the other associations.
-
Kane
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
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
- 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 Model Namespaces and HABTM looks more promising to me.
-
Santiago Pastorino
- 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
- State changed from open to stale