# Habtm many-to-many relationship problem attribute\_methods.rb

**URL:** <https://www.ruby-forum.com/t/habtm-many-to-many-relationship-problem-attribute-methods-rb/141970>\
**Category:** Rails\
**Created:** [July 2, 2008, 6:57am UTC](https://www.ruby-forum.com/t/habtm-many-to-many-relationship-problem-attribute-methods-rb/141970 "2008-07-02T06:57:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![c-a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac8455/32.png) [@c-a](https://www.ruby-forum.com/u/c-a)\
**Post date:** [July 2, 2008, 6:57am UTC](https://www.ruby-forum.com/t/habtm-many-to-many-relationship-problem-attribute-methods-rb/141970/1 "2008-07-02T06:57:54Z")

</div>

Hi,

I have a many-to-many relationship between: registrations and groups  
(properly setup on each side with has\_and\_belongs\_to\_many)

so I do this in the console:

reg = Registration.find(:first)  
grp = Group.find(:first)

grp.registrations list me the registrations’ attributes linked with  
the group in question

reg.groups returns me:

NoMethodError: undefined method `groups' for #<Registration:0x3164718> from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/ active_record/attribute_methods.rb:256:in`method\_missing’  
from (irb):XX

where XX started around 10 I think and kept augmenting each time I ran  
“reg.groups”

I can’t figure out what is happening to get this message…  
registrations and groups are identically setup to what I can see…

thanks if anyone can bring me any clue!

PS. I’m using restful\_authentication and restful\_acl, but it shouldn’t  
give any problem here…

---

<div class="post-metadata">

**Author:** ![c-a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac8455/32.png) [@c-a](https://www.ruby-forum.com/u/c-a)\
**Post date:** [July 2, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/habtm-many-to-many-relationship-problem-attribute-methods-rb/141970/2 "2008-07-02T15:44:32Z")

</div>

Actually, some model names are in french so I translated, so it could  
make more sens … but registration is inscription and groups are  
periodes in the above example…

some code in the models are temporary, just so that I can debug more  
easily.  
Here are models and migrations

#########################################  
class Inscription \< ActiveRecord::Base

has\_and\_belongs\_to\_many :periodes  
belongs\_to :user

#restful\_acl part - supposed to work fine###############  
def is\_updatable\_by(user)  
current\_user.is\_admin?  
end

def is\_deletable\_by(user)  
current\_user.is\_admin?  
end

def self.is\_readable\_by(user, object = nil)  
true  
#current\_user.is\_admin?  
end

def self.is\_creatable\_by(user)  
current\_user.is\_admin?  
#user != nil #Ensure the user is logged in

end

end

######################################  
class Periode \< ActiveRecord::Base

has\_and\_belongs\_to\_many :inscriptions

belongs\_to :course  
belongs\_to :prof

def is\_updatable\_by(user)  
current\_user.is\_admin?  
end

def is\_deletable\_by(user)  
current\_user.is\_admin?  
end

def self.is\_readable\_by(user, object = nil)  
true  
#user != nil #Ensure the user is logged in  
end

def self.is\_creatable\_by(user)  
current\_user.is\_admin?  
#user != nil #Ensure the user is logged in

end

end

other models  
##########  
user has\_many :inscriptions  
prof has\_many :periodes  
course has\_many :periodes

##############################################  
MIGRATIONS  
#############################################

class CreatePeriodes \< ActiveRecord::Migration  
def self.up  
create\_table :periodes do |t|  
t.integer :course\_id  
t.integer :prof\_id

```
  t.timestamps
end

```

end

def self.down  
drop\_table :periodes  
end  
end

#################  
class CreateInscriptionsPeriodes \< ActiveRecord::Migration  
def self.up  
create\_table :inscriptions\_periodes, :id =\> false do |t|  
t.integer :inscription\_id  
t.integer :periode\_id

```
  t.timestamps
end

#add_index :inscriptions_periodes, [:inscription_id, :periode_id]

```

end

def self.down  
drop\_table :inscriptions\_periodes  
end  
end

##### 

class CreateInscriptions \< ActiveRecord::Migration  
def self.up  
create\_table :inscriptions do |t|  
t.integer :user\_id

```
  t.timestamps
end

```

end

def self.down  
drop\_table :inscriptions  
end  
end
