hello, I''am new to ROR and I have a problem. I have to models "rights" and "roles" on a has_and_belongs_to_many relationship, through "roles_rights" table. When I try to add a new role although I put in my form fields for each right, a record is added to table roles but none to roles_rights. I am using Ruby 1.8.4 rails 1.0 mysql 5.0 here is my code: ===========================controller: ===========================def create @role = Role.new(params[:role]) @role.rights = Right.find(@params[:right_ids])if @params[:right_ids] if @role.save flash[:notice] = ''Role was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end ===========================view: ===========================<p><label for="role_name">Name</label><br/> <%= text_field ''role'', ''name'' %></p> <div><label for="role_right">Rights</label><br/> <ul> <% for right in @rights %> <li><input type="checkbox". id ="<%= right.id %>". name="right_ids[]". value="<%= right.id %>". > <%= right.name %></li> <% end %> </ul> =================================model: =================================class Role < ActiveRecord::Base has_and_belongs_to_many :users has_and_belongs_to_many :rights, :join_table => ''roles_rights'' end class Right < ActiveRecord::Base has_and_belongs_to_many :roles, :join_table => ''roles_rights'' end -- Posted via http://www.ruby-forum.com/.
the way collections work is that 1) they are saved immediately if the parent record is not a new record or 2) are saved when the parent record is saved. your situation clearly falls under #2. so they should be save when you save the @role record. since this isn''t happening, it''s possible that there are no rights in the collection. i might suggest adding a breakpoint after @role.rights = Right.find(@params[:right_ids])if @params[:right_ids] run script/breakpointer in a shell then load the app and create a new role. in the breakpointer, you can then examine the vars to make sure they have the values you expect them to have. CTRL-D to end the breakpoint session. @params[:right_ids] @role.rights On 2/15/06, Bogdan Nenu <bogdan.nenu@gmail.com> wrote:> > hello, > > I''am new to ROR and I have a problem. > I have to models "rights" and "roles" on a has_and_belongs_to_many > relationship, through "roles_rights" table. When I try to add a new role > although I put in my form fields for each right, a record is added to > table roles but none to roles_rights. > > I am using Ruby 1.8.4 rails 1.0 mysql 5.0 > > here is my code: > ===========================> controller: > ===========================> def create > @role = Role.new(params[:role]) > @role.rights = Right.find(@params[:right_ids])if @params[:right_ids] > if @role.save > flash[:notice] = ''Role was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > ===========================> view: > ===========================> <p><label for="role_name">Name</label><br/> > <%= text_field ''role'', ''name'' %></p> > > <div><label for="role_right">Rights</label><br/> > <ul> > <% for right in @rights %> > <li><input type="checkbox". > id ="<%= right.id %>". > name="right_ids[]". > value="<%= right.id %>". > > <%= right.name %></li> > > <% end %> > </ul> > > =================================> model: > =================================> class Role < ActiveRecord::Base > has_and_belongs_to_many :users > has_and_belongs_to_many :rights, :join_table => ''roles_rights'' > end > > class Right < ActiveRecord::Base > has_and_belongs_to_many :roles, :join_table => ''roles_rights'' > end > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060215/c16e3759/attachment.html
Bogdan Nenu wrote:> hello, > > I''am new to ROR and I have a problem. > I have to models "rights" and "roles" on a has_and_belongs_to_many > relationship, through "roles_rights" table. When I try to add a new role > although I put in my form fields for each right, a record is added to > table roles but none to roles_rights.10 to 1 this is the model with HABTO < 1 bug. Theres a fix you have to apply to active record A quick google will find it for you. -- Posted via http://www.ruby-forum.com/.
On Feb 15, 2006, at 16:25, AC Green wrote:> Bogdan Nenu wrote: >> hello, >> >> I''am new to ROR and I have a problem. >> I have to models "rights" and "roles" on a has_and_belongs_to_many >> relationship, through "roles_rights" table. When I try to add a >> new role >> although I put in my form fields for each right, a record is added to >> table roles but none to roles_rights. > > > 10 to 1 this is the model with HABTO < 1 bug. > > Theres a fix you have to apply to active record > > A quick google will find it for you.I need to be aware of this gotcha. Would you please provide some keywords to search for? "HABTO active record" returned nothing relevant. -- fxn
Bogdan Nenu
2006-Feb-16 13:17 UTC
[Rails] Re: Re: problem with saving id''s in a join table
Xavier Noria wrote:> On Feb 15, 2006, at 16:25, AC Green wrote: > >> >> 10 to 1 this is the model with HABTO < 1 bug. >> >> Theres a fix you have to apply to active record >> >> A quick google will find it for you. > > I need to be aware of this gotcha. Would you please provide some > keywords to search for? "HABTO active record" returned nothing relevant. > > -- fxn@params[:rights_ids] came OK, but the @role.rights does not seem to be ok for some reason. It might be some problem with my line: @role.rights = Right.find(@params[:right_ids]) if @params[:right_ids] -- Posted via http://www.ruby-forum.com/.
Seemingly Similar Threads
- Storing order of sortable objects in RoR
- Diagnosing trouble with R-2.0, Fedora Core 2, and Rcmdf
- Using Scalar Evolution to Identify Expressions Evolving in terms of Loop induction variables
- ActionView::Base.field_error_proc not getting an error field
- Write/Display AR query as Grouped Results?