Nested attributes (unknown attribute error)-overiding default foreign key referencing

poornima c.
Fri, 08 Feb 2013 11:00:01 -0000

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