Forum: Ruby on Rails How can i use info of two or more tables in the one form

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.
reynaldo hernandez (Guest)
on 2006-04-21 19:46
i have a trouble with my application, because i need to load three
combobox, called employes, customer, products, but i cannot to access
them.

i need a model for each one them?

how can i access to the info of the three tables: employes, customers,
products, to show in a view..

please

help me...

thanks
Alex W. (Guest)
on 2006-04-21 20:24
reynaldo hernandez wrote:
> i have a trouble with my application, because i need to load three
> combobox, called employes, customer, products, but i cannot to access
> them.
>
> i need a model for each one them?
>
> how can i access to the info of the three tables: employes, customers,
> products, to show in a view..
>
> please
>
> help me...
>
> thanks

Are you trying to assign an employee, customer, and a product to some
other model?  Such as this?

class Post < ActiveRecord::Base
  has_one :employee
  has_one :customer
  has_one :product
end

If so then is not too hard.

<%= select 'post', 'employee_id',
Empolyee.find(:all).collect {|emp| [emp.name, emp.id]} %>

<%= select 'post', 'customer_id',
Customer.find(:all).collect {|cus| [cus.name, cus.id]} %>

<%= select 'post', 'product_id',
Product.find(:all).collect {|prod| [prod.name, prod.id]} %>

You can simply call you find methods on your classes from your view.

Or if you actually want three different models in your view:

#Controller
def new
  @employee = Employee.new
  @customer = Customer.new
  @product  = Product.new
end

def create
  Employee.create(params[:employee])
  Customer.create(params[:customer])
  Product.create(params[:product])
end

#View new.rhtml
<%= form_tag :action => 'create' %>
  <%= text_field 'employee', 'name' %>
  <%= text_field 'customer', 'email' %>
  <%= text_field 'product', 'price' %>
  <%= submit_tag 'Create' %>
<%= end_form_tag %>
This topic is locked and can not be replied to.