Dhaval Phansalkar
2009-Aug-25 17:45 UTC
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| -%>
<p><label for="login">Login</label><br/>
<%= f.text_field :login %></p>
<p><label for="email">Email</label><br/>
<%= f.text_field :email %></p>
<p><label for="password">Password</label><br/>
<%= f.password_field :password %></p>
<p><label for="password_confirmation">Confirm
Password</label><br/>
<%= f.password_field :password_confirmation %></p>
<p><label
for="password_confirmation">Applications</label><br/>
<%for app in @apps%>
<%= check_box_tag "user[app_ids][]", app.id,
@user.apps.include?(app)%> <%=app.name%><br/>
<%end%>
</p>
<p><%= submit_tag ''Sign up'' %></p>
<% end -%>
log:
Parameters:
{"user"=>{"password_confirmation"=>"aaaa",
"app_ids"=>["1",
"2", "3", "4"],
"login"=>"aaaa",
"password"=>"aaaa",
"email"=>"aaaa-MB/4cHSNlRU@public.gmane.org"},
"commit"=>"Sign up",
"action"=>"create",
"controller"=>"users"}
--
Posted via http://www.ruby-forum.com/.
Rails List
2009-Aug-26 11:18 UTC
Re: unable to create record with has_mant :through relationship
Dhaval Phansalkar 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? -- Posted via http://www.ruby-forum.com/.
Rails List
2009-Aug-26 11:22 UTC
Re: unable to create record with has_mant :through relationship
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 -- Posted via http://www.ruby-forum.com/.
Rails List
2009-Aug-26 11:23 UTC
Re: unable to create record with has_mant :through relationship
Rails List 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 > endlet me know how it goes http://www.craigslistcloned.com -- Posted via http://www.ruby-forum.com/.
Dhaval Phansalkar
2009-Aug-26 11:28 UTC
Re: unable to create record with has_mant :through relationship
Rails List wrote:> Rails List 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.comyes, thats correct. but i''m going to add one more field called permission in the join table. so used has_many :through -- Posted via http://www.ruby-forum.com/.
Rails List
2009-Aug-26 11:47 UTC
Re: unable to create record with has_mant :through relationship
>> >> 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 :throughHave you got a corresponding model for Categorization?. -- Posted via http://www.ruby-forum.com/.
Dhaval Phansalkar
2009-Aug-26 11:51 UTC
Re: unable to create record with has_mant :through relationship
Rails List 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 -- Posted via http://www.ruby-forum.com/.
Rick DeNatale
2009-Aug-26 14:29 UTC
Re: unable to create record with has_mant :through relationship
On Tue, Aug 25, 2009 at 1:45 PM, Dhaval Phansalkar<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.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> -uQpvtAwYA0+zQB+pC5nmwQ@public.gmane.org! > redirect_to(:action => "new") > flash[:notice] = "Thanks for signing up!" > rescue ActiveRecord::RecordInvalid > render :action => ''new'' > end > endHTH -- 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
Dhaval Phansalkar
2009-Aug-27 05:46 UTC
Re: unable to create record with has_mant :through relationship
Rick Denatale wrote:> On Tue, Aug 25, 2009 at 1:45 PM, Dhaval > Phansalkar<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 >> � �-uQpvtAwYA0+zQB+pC5nmwQ@public.gmane.org! >> � �redirect_to(:action => "new") >> � �flash[:notice] = "Thanks for signing up!" >> � �rescue ActiveRecord::RecordInvalid >> � �render :action => ''new'' >> �end >> end> Rick DeNataleThanks 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 -- Posted via http://www.ruby-forum.com/.