# Has\_many through problem

**URL:** <https://www.ruby-forum.com/t/has-many-through-problem/171461>\
**Category:** Rails\
**Created:** [July 2, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/has-many-through-problem/171461 "2009-07-02T22:00:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_DSoSSSSjgi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c67d28/32.png) [@Lee\_DSoSSSSjgi](https://www.ruby-forum.com/u/Lee_DSoSSSSjgi)\
**Post date:** [July 2, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/has-many-through-problem/171461/1 "2009-07-02T22:00:03Z")

</div>

Hi,

I have pretty generic user/role models linked through an Authorization  
model  
using has\_many :through =\> … but I’m having difficulty getting it to  
work  
the way I expect it to. When I instantiate a new role I can see it’s  
attributes via to\_yaml(), but when I call it’s name() method I get nil.  
I’m  
baffled; below is a short code snippet - can anyone see what the problem  
is?

class Authorization \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :role  
end

class Role \< ActiveRecord::Base  
has\_many :authorizations  
has\_many :users, :through =\> :authorizations  
attr\_accessor :id, :name, :description  
DEFAULT = 4  
end

class User \< ActiveRecord::Base  
has\_many :authorizations  
has\_many :roles, :through =\> :authorizations  
attr\_accessible :id, :sso\_id, :first\_name, :last\_name, :email

def self.get\_or\_create\_from\_sso\_user(sso\_user)  
user = self.find\_by\_sso\_id(sso\_user.id)  
if user  
return user  
else  
# Create user  
created\_user = self.create(  
:sso\_id =\> sso\_user.id,  
:first\_name =\> sso\_user.first\_name,  
:last\_name =\> sso\_user.last\_name,  
:email =\> sso\_user.email  
)  
Authorization.create! :user\_id =\> created\_user.id, :role\_id =\>  
Role::DEFAULT  
created\_user.reload  
created\_user.save!  
end  
end

def has\_admin\_role?  
self.roles.each do |role|  
puts role.to\_yaml # see the output for this below  
puts role.name # see the output for this below  
return true if role.name.eql? ‘admin’  
end  
return false  
end

def sso\_user  
@sso\_user ||= SSO::User.find(self.id)  
end  
end

The output for the two puts() above gives me this:  
– begin –  
— !ruby/object:Role  
attributes:  
name: requester  
updated\_at:  
id: “4”  
description: Default role for all JGI users  
created\_at:  
attributes\_cache: {}

nil  
– end –

I also seem to have a problem with the role class itself:

– BEGIN CONSOLE –

> > role = Role.find\_by\_id(1)  
> > =\> #\<Role id: 1, name: “admin”, description: “Administrator (global  
> > read/write privileges)”, created\_at: nil, updated\_at: nil\>
> 
> > role.name  
> > =\> nil

---

<div class="post-metadata">

**Author:** ![Lee\_DSoSSSSjgi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c67d28/32.png) [@Lee\_DSoSSSSjgi](https://www.ruby-forum.com/u/Lee_DSoSSSSjgi)\
**Post date:** [July 2, 2009, 10:18pm UTC](https://www.ruby-forum.com/t/has-many-through-problem/171461/2 "2009-07-02T22:18:45Z")

</div>

Creating roles is also an issue… I’m baffled!

=\> #\<Role id: nil, name: nil, description: nil, created\_at: nil,  
updated\_at:  
nil\>

> > role.name = ‘test’  
> > =\> “test”
> 
> > role.save!  
> > =\> true
> 
> > role  
> > =\> #\<Role name: nil, description: nil, created\_at: “2009-07-02  
> > 13:04:44”,  
> > updated\_at: “2009-07-02 13:04:44”\>

Here’s my migration:

require ‘active\_record/fixtures’

class CreateRoles \< ActiveRecord::Migration  
def self.up  
create\_table :roles do |t|  
t.string :name  
t.string :description  
t.timestamps  
end

```
Fixtures.create_fixtures('db/fixtures', File.basename("roles.yml",

```

‘.\*’))  
end

def self.down  
drop\_table :roles  
end  
end
