Unable to create record with has_mant :through relationship

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”=>“[email protected]”}, “commit”=>“Sign up”, “action”=>“create”,
“controller”=>“users”}

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?

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

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

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

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

let me know how it goes

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?.

Rails L. wrote:

let me know how it goes

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

On Tue, Aug 25, 2009 at 1:45 PM, Dhaval
Phansalkar[email protected] 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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Rick Denatale wrote:

On Tue, Aug 25, 2009 at 1:45 PM, Dhaval
Phansalkar[email protected] 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 :slight_smile:

-DPP