# Unable to create record with has\_mant :through relationship

**URL:** <https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365>\
**Category:** Rails\
**Created:** [August 25, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365 "2009-08-25T19:45:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 25, 2009, 7:45pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/1 "2009-08-25T19:45:54Z")

</div>

hi all,

i have a has\_many :through r’ship in my application. my tables are -  
users, apps(short form for applications), categorizations. i’m trying to  
create a user in new.rhtml where all apps are also shown in checkboxes  
for selection. on submit, user gets created but the join table i.e.  
categorizations table doesn’t get updated. where am i going wrong?  
doesn’t the join table gets updated automatically? or do i’ve to update  
it manually? if yes, how? Any help will be greatly appreciated. thanks  
in advance.

code:

class App \< ActiveRecord::Base  
has\_many :categorizations  
has\_many :users, :through =\> :categorizations  
end

class User \< ActiveRecord::Base  
has\_many :categorizations  
has\_many :apps, :through =\> :categorizations  
end

class Categorization \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :app  
end

class UsersController \< ApplicationController  
def new  
@user = User.new  
@apps = App.find(:all)  
end

def create  
@user = User.new(params[:user])  
@user.save!  
redirect\_to(:action =\> “new”)  
flash[:notice] = “Thanks for signing up!”  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘new’  
end  
end

users/new.rhtml  
\<%= error\_messages\_for :user %\>  
\<% form\_for :user, :url =\> users\_path do |f| -%\>

Login  
\<%= f.text\_field :login %\>

Email  
\<%= f.text\_field :email %\>

Password  
\<%= f.password\_field :password %\>

Confirm Password  
\<%= f.password\_field :password\_confirmation %\>

Applications  
\<%for app in @apps%\> \<%= check\_box\_tag "user[app\_ids][]", app.id, @user.apps.include?(app)%\> \<%=app.name%\>  
\<%end%\>

\<%= submit\_tag 'Sign up' %\>

\<% end -%\>

log:  
Parameters: {“user”=\>{“password\_confirmation”=\>“aaaa”, “app\_ids”=\>[“1”,  
“2”, “3”, “4”], “login”=\>“aaaa”, “password”=\>“aaaa”,  
“email”=\>“removed\_email\_address@domain.invalid”}, “commit”=\>“Sign up”, “action”=\>“create”,  
“controller”=\>“users”}

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 26, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/2 "2009-08-26T13:18:04Z")

</div>

Dhaval P. wrote:

> hi all,
> 
> i have a has\_many :through r’ship in my application. my tables are -  
> users, apps(short form for applications), categorizations. i’m trying to  
> create a user in new.rhtml where all apps are also shown in checkboxes  
> for selection. on submit, user gets created but the join table i.e.  
> categorizations table doesn’t get updated. where am i going wrong?  
> doesn’t the join table gets updated automatically? or do i’ve to update  
> it manually? if yes, how? Any help will be greatly appreciated. thanks  
> in advance.
> 
> code:

where is your habtm?

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 26, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/3 "2009-08-26T13:22:46Z")

</div>

An User can have many applications and an App can have many users. So I  
would think…

class App \< ActiveRecord::Base  
has\_and\_belongs\_to\_many: users  
end

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many: apps  
end

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 26, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/4 "2009-08-26T13:23:37Z")

</div>

Rails L. wrote:

> An User can have many applications and an App can have many users. So I  
> would think…
> 
> class App \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many: users  
> end
> 
> class User \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many: apps  
> end

let me know how it goes

[http://www.craigslistcloned.com](http://www.craigslistcloned.com)

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 26, 2009, 1:28pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/5 "2009-08-26T13:28:07Z")

</div>

Rails L. wrote:

> Rails L. wrote:
> 
> > An User can have many applications and an App can have many users. So I  
> > would think…
> > 
> > class App \< ActiveRecord::Base  
> > has\_and\_belongs\_to\_many: users  
> > end
> > 
> > class User \< ActiveRecord::Base  
> > has\_and\_belongs\_to\_many: apps  
> > end
> 
> let me know how it goes
> 
> [http://www.craigslistcloned.com](http://www.craigslistcloned.com)

yes, thats correct. but i’m going to add one more field called  
permission in the join table. so used has\_many :through

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 26, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/6 "2009-08-26T13:47:15Z")

</div>

> > let me know how it goes
> > 
> > [http://www.craigslistcloned.com](http://www.craigslistcloned.com)
> 
> yes, thats correct. but i’m going to add one more field called  
> permission in the join table. so used has\_many :through

Have you got a corresponding model for Categorization?.

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 26, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/7 "2009-08-26T13:51:15Z")

</div>

Rails L. wrote:

> > > let me know how it goes
> > > 
> > > [http://www.craigslistcloned.com](http://www.craigslistcloned.com)
> > 
> > yes, thats correct. but i’m going to add one more field called  
> > permission in the join table. so used has\_many :through
> 
> Have you got a corresponding model for Categorization?.

yes. it’s in my 1st post under “code”  
class Categorization \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :app  
end

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 26, 2009, 4:31pm UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/8 "2009-08-26T16:31:15Z")

</div>

On Tue, Aug 25, 2009 at 1:45 PM, Dhaval  
Phansalkar[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in advance.  
> I beleive that you need to create the categorizations yourself  
> something like (this is untested):

> class UsersController \< ApplicationController  
> def new  
> @user = User.new  
> @apps = App.find(:all)  
> end
> 
> def create  
> @user = User.new(params[:user])  
> params[:user][:app\_ids].each do | app\_id|  
> @user.categorizations \<\< Category.new(:app\_id =\> app\_id)  
> end  
> @user.save!  
> redirect\_to(:action =\> “new”)  
> flash[:notice] = “Thanks for signing up!”  
> rescue ActiveRecord::RecordInvalid  
> render :action =\> ‘new’  
> end  
> end

HTH

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![dhaval\_parikh33](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@dhaval\_parikh33](https://www.ruby-forum.com/u/dhaval_parikh33)\
**Post date:** [August 27, 2009, 7:46am UTC](https://www.ruby-forum.com/t/unable-to-create-record-with-has-mant-through-relationship/174365/9 "2009-08-27T07:46:17Z")

</div>

Rick Denatale wrote:

> On Tue, Aug 25, 2009 at 1:45 PM, Dhaval  
> Phansalkar[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > in advance.  
> > I beleive that you need to create the categorizations yourself  
> > something like (this is untested):
> 
> > class UsersController \< ApplicationController  
> > ï¿½def new  
> > ï¿½ ï¿½@user = User.new  
> > ï¿½ ï¿½@apps = App.find(:all)  
> > ï¿½end
> > 
> > ï¿½def create  
> > ï¿½ ï¿½@user = User.new(params[:user])  
> > params[:user][:app\_ids].each do | app\_id|  
> > @user.categorizations \<\< Category.new(:app\_id =\> app\_id)  
> > end  
> > ï¿½ ï¿½@user.save!  
> > ï¿½ ï¿½redirect\_to(:action =\> “new”)  
> > ï¿½ ï¿½flash[:notice] = “Thanks for signing up!”  
> > ï¿½ ï¿½rescue ActiveRecord::RecordInvalid  
> > ï¿½ ï¿½render :action =\> ‘new’  
> > ï¿½end  
> > end

> Rick DeNatale

Thanks Rick. That worked. i thought join table might get updated  
automatically after mentioning the r’ship in models. only 1 change in ur  
code, Category shd be replaced with Categorization. Thanks once again 🙂

-DPP
