ruby - rails: accessing table from has_and_belongs_to_many relationship -


activerecord::base.connection.tables

["groups", "groups_users", "members", "questions", "users", "votes", "schema_migrations"] 

as can tell, have table named, "groups_users" yet cannot seem access it. can access "users" table user. can access "groups" table group. how access "groups_users"? have tried:

groups_users groups_user groupsusers groupsuser 

i have tried singular version of groups. ideas on how access it? documentation help.

group

class group < activerecord::base   has_and_belongs_to_many :users end 

user

class user < activerecord::base   has_and_belongs_to_many :groups end 

you have define model groups_users table if want access it

class groupuser < activerecord::base   self.table_name = "groups_users"   belongs_to :user   belongs_to :group end 

you can define associations this

class group < activerecord::base   has_many :group_users   has_many :users, :through => :group_users end   class user< activerecord::base   has_many :group_users   has_many :groups, :through => :group_users end 

you can read difference here


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -