I am trying to do a similar thing for some spares that I am trying to manage...
My spare.rb file is as under
class Spare < ActiveRecord::Base
belongs_to :department, :class_name => ''department'',
:foreign_key
=> ''department_id''
belongs_to :subequipment, class_name => ''subequipment'',
:foreign_key => ''sub_equipment''
<snip>
My view reads
<snip>
<p><label
for="spare_department">Department</label><br/>
<select name="spare[department_id]">
<% @departments.each do |department_id| %>
<option value="<%= department_id.id %>">
<%= department_id.department_name %>
</option>
<% end %>
</select>
</p>
<p><label for="spare_sub_equipment">Sub
equipment</label><br/>
<select name="spare[subequipment]">
<% @subequipments.each do |subequipment| %>
<option value="<%= subequipment.id %>">
<%= subequipment.sub_equipment_name %>
</option>
<% end %>
</select>
</p>
<snip>
It works fine if I have only one foreign key say department. When I
try to introduce a second one like sub-equipment by adding the line
belongs_to :subequipment, class_name => ''subequipment'',
:foreign_key
=> ''sub_equipment''
I get the following error
ArgumentError in Admin#create Unknown key(s): Spare
RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace |
Full Trace
C:/InstantRails_1_0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/core_ext/hash/keys.rb:48:in
`assert_valid_keys''
C:/InstantRails_1_0/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:519:in
`belongs_to_without_reflection''
(eval):5:in `belongs_to''
#{RAILS_ROOT}/app/models/spare.rb:6
#{RAILS_ROOT}/app/controllers/admin_controller.rb:24:in `create''
Request
Parameters: {"commit"=>"Create",
"spare"=>{"nature"=>"C",
"remarks"=>"",
"pattern_number"=>"mbm-50",
"description"=>"akjl",
"subequipment"=>"1",
"equipment"=>"1",
"department_id"=>"1",
"section"=>"1"},
"location"=>{"box_number"=>"",
"compartment"=>"",
"cell_number"=>""}}
Show session dump
---
flash: !map:ActionController::Flash::FlashHash {}
Response Headers: {"cookie"=>[],
"Cache-Control"=>"no-cache"}
On 3/22/06, Liquid <has.sox@gmail.com> wrote:>
> Hi,
>
> In your model you could explicitly state the relationship
>
> class Student < ActionRecord::Base
> belongs_to :department, :class_name="department", :foriegn_key
=> ''department_id''
> end
>
> Then in the veiw use
> <%= text_field "student", "department_id" %>
>
> or similar for a select box.
>
> I had similar problems when I was trying to get a form working and found
that by doing this it worked. I think there is supposed to be some rails magic
that does this for you but I don''t seem to have much luck with it.
>
> Cheers
>
>
>
> On 3/22/06, Andy Shen <andy.s.shen@gmail.com> wrote:
> > Hi,
> >
> > I think even if you use integer you will still get similar error,
> > except it will say Fixnum instead of string.
> > Because it seems like :department should be of object Department, not
> > the foreign key.
> >
> > Cheers,
> > Andy
> >
> > On 3/22/06, Preethi Naveen <preethi.naveen@gmail.com> wrote:
> > >
> > > Hello,
> > >
> > > I am trying to create a new record in a database by modifying the
create
> > > method generated by scaffold. I have a situation where i want to
add new
> > > student and while creating a new student, i am able to choose the
department
> > > the student belongs to. However when i say create, i get an error
saying
> > > that Department expected, got string.
> > >
> > > Parameters: {"commit"=>"Create",
"student"=>{"department"=>"1"}
> > >
> > > the method
> > >
> > > def create
> > > @student = Student.new(params[:student]) #error caused at
this line
> > > if @student.save
> > > :<snip>
> > > end
> > >
> > > The department field is of type integer
> > > so it looks like it expects an integer value while department
returns a
> > > string. How do i convert it to an integer so that i can
succesfully add a
> > > new student to the database
> > >
> > > thanks
> > >
> > > Naveen
> > > _______________________________________________
> > > Rails mailing list
> > > Rails@lists.rubyonrails.org
> > > http://lists.rubyonrails.org/mailman/listinfo/rails
> > >
> > >
> > >
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
--
Dattatraya
-------------------------------------------------
Work fascinates me, I can stare at it for hours...!!