poornima c.
2013-Feb-08 11:00 UTC
Nested attributes (unknown attribute error)-overiding default foreign key referencing
Am getting the following error in ruby on rails: ActiveRecord::UnknownAttributeError in AWRequestsController#new unknown attribute: a_w_request_id. I have a form that is tied to two models. a_w_request.rb: class AWRequest <ActiveRecord::Base set_primary_key : request_id set_table_name: w_r_details attr_accessible : request_id, :requester_contact,:needed_by,:w_a_details_attributes has many: w_a_details, :dependent => :destroy accepts_nested_attributes for :w_a_details, :allow_destroy => :true :reject_if => proc {|attrs| attrs.all? {|k,v| v.blank? } } end w_a_detail.rb: Class WADetail < ActiveRecord::Base set_primary_key :w_a_detail_id set_table_name "w_a_details" attr_accessible :description,:request_id,:w_a_detail_id belong_to :a_w_request, :foreign_key "request_id" end I do not have an attribute a_w_request_id in my table which acts as foreign key instead i have request_id as foreign key in w_a_details table which references request_id primary key in w_r_details table that uses a_w_request model(specified using set_table_name property). Am using oracle as my backend. I get the error in the following line in my controller @a_w_request.w_a_details.build Below is my controller''s new and create methods: def new @a_w_request=AWRequest.new @a_w_request.w_a_details.build respond_to do |format| format.html format.json {render json:@a_w_request} end end def create @a_w_request=AWRequest.new(params[:a_w_request]) respond to do |format| if @a_w_request.save format.html{redirect_to @a_w_request,notice:''request successfuly created''} format.json{render json: @a_w_request, status :created, location :@a_w_request} else format.html {render action: "new"} format.json { render json :@a_w_request.errors, status: :unprocessable entity} end end end Here is my form: /a_w_request/_form.html.erb: <%=nested_form_for(@a_w_request) do |f| %> <%= f.text_field :requester_contact %> <%= f.date_select :needed_by %> <%= render :partial=>''w_a_details/form'',:locals=>{:form=>f} %> <%= f.link_to_add "add request", :w_a_details %> <%= f.submit %> <% end %> /w_a_details/_form.html.erb <%=form.fields_for :w_a_details do |a| %> <%=a.text_field :description %> <%=a.link_to_remove "remove" %> <% end %> -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Feb-08 12:30 UTC
Re: Nested attributes (unknown attribute error)-overiding default foreign key referencing
On 8 February 2013 11:00, poornima c. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Am getting the following error in ruby on rails: > ActiveRecord::UnknownAttributeError in AWRequestsController#new unknown > attribute: a_w_request_id. > > I have a form that is tied to two models. > > a_w_request.rb: > > class AWRequest <ActiveRecord::Base > set_primary_key : request_id > set_table_name: w_r_details > attr_accessible : request_id, > :requester_contact,:needed_by,:w_a_details_attributes > > has many: w_a_details, :dependent => :destroy > > accepts_nested_attributes for :w_a_details, :allow_destroy => :true > :reject_if => proc {|attrs| attrs.all? {|k,v| v.blank? } } > end > > w_a_detail.rb: > > Class WADetail < ActiveRecord::Base > set_primary_key :w_a_detail_id > set_table_name "w_a_details" > attr_accessible :description,:request_id,:w_a_detail_id > belong_to :a_w_request, :foreign_key "request_id"That should be belongs_to not belong_to and foreign_key: "request_id" or :foreign_key => "request_id" Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
poornima c.
2013-Feb-08 17:19 UTC
Re: Nested attributes (unknown attribute error)-overiding default foreign key referencing
Hi Colin Sorry for the typo error. My code has belongs_to and :foreign_key=>"request_id" . Still it throws this error. Looks like I have to use alias_attribute functionality to map asin_whitelist_request_id to request_id in my table. But am not sure about how to handle the queries. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.