Forum: Ruby on Rails need help with controller/show.html.erb

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael R (Guest)
on 2009-04-20 11:29
(Received via mailing list)
Hello,

I am attempting to display data in the requseraccount form, from the
divisions table.

For example: I have a division_id field in requseraccount that works
great. No issues getting this populated from my create form. The
problem is that I don't want to display the id when showing the form.
I want to display the division_name, which is in the division table.

Here is a copy of my code
requseraccounts_controller.rb and schema.rb: http://dpaste.com/35716/
requseraccoutns: show.html.erb:  http://dpaste.com/35717/
SQL Query: http://dpaste.com/35725/

I get the following error when I try the above code:

 ActiveRecord::RecordNotFound in RequseraccountsController#show

   Couldn't find Division with ID=8

Which makes sense, since it is trying to do a query WHERE
division_id=8. I need the query to use requseaccount.divisions_id
(which is 2) and not requseraccount.id (which is 8).

Any advice would be much appreciated.

Michael
Paco G. (Guest)
on 2009-04-20 14:41
Hi Michael

You have to specify the association between division and requseraccount
models

In your models directory:

division.rb

class Division < ActiveRecord::Base
  has_many :requseraccount
end

class Requseraccount < ActiveRecord::Base
  belongs_to :division
end

And now you can do the following in your controller and view:

@req = Requseraccount.find(:all, :include => :division)
@req.division.name # "division_name"

You can read more about it at http://guides.rubyonrails.org/. And about
associations in http://guides.rubyonrails.org/association_basics.html

Regards


Michael R wrote:
> Hello,
>
> I am attempting to display data in the requseraccount form, from the
> divisions table.
>
> For example: I have a division_id field in requseraccount that works
> great. No issues getting this populated from my create form. The
> problem is that I don't want to display the id when showing the form.
> I want to display the division_name, which is in the division table.
>
> Here is a copy of my code
> requseraccounts_controller.rb and schema.rb: http://dpaste.com/35716/
> requseraccoutns: show.html.erb:  http://dpaste.com/35717/
> SQL Query: http://dpaste.com/35725/
>
> I get the following error when I try the above code:
>
>  ActiveRecord::RecordNotFound in RequseraccountsController#show
>
>    Couldn't find Division with ID=8
>
> Which makes sense, since it is trying to do a query WHERE
> division_id=8. I need the query to use requseaccount.divisions_id
> (which is 2) and not requseraccount.id (which is 8).
>
> Any advice would be much appreciated.
>
> Michael
This topic is locked and can not be replied to.