Topic: Foreign Key fields not addressed in views

Hi. I am definitely a newbee to RoR. I'm looking at porting a Web application from Java (using JDeveloper and Oracle's lightweight ADF data connections. I'm having a very basic problem in understanding table relationships under RoR (using MySQL).

The app is an HR system, allowing multiple client companies, with each company having numerous employees, and each employee having several sub-tables of various data (i.e. dependents, benefits, payroll deductions, training courses, jobs etc).

I set up the client (against "clients" table with an 'id') model with "has_many :employees:. I set up the employee model (against "employees" table with and 'id' and a fk 'client_id') as
"belongs_to :client".

I initially used controllers with dynamic "scaffolding". I then restarted after "generating" scaffolds in order to actually see more of what was going on. I've tried it both with and without the foreign keys defined in the database tables.

The views resulting from scaffolding do NOT reflect the foreign key fields.

To create a new employee, how do I assign the client_id? The model, unlike my Java and other experience, does not reflect what fields are there. Is the foreign key field there? Included in the model at runtime? Is is editable?

If I wanted a master-detail form (i.e. a listing of the top-level table of the several client companies and, upon selection, a detail listing of all employees of that company) how can I get one readily?

I know this is probably a VERY base issue and I'm sorry to seem soooo stupid. I'm sure it's a factor of my prior experience and vantage point and I just can't readily grasp the RoR view. But I sure would like to. I'm already loving RoR.

Thanks

Re: Foreign Key fields not addressed in views

m.kruger wrote:

The views resulting from scaffolding do NOT reflect the foreign key fields.

Scaffolding is very simplistic. It is never intended to handle relationships and foreign keys. It just shows how to handle simple one-model CRUD operations.


m.kruger wrote:

To create a new employee, how do I assign the client_id? The model, unlike my Java and other experience, does not reflect what fields are there. Is the foreign key field there? Included in the model at runtime? Is is editable?

The foreign key is a normal field like any other, the scaffolding just chooses not to display it. You can set it like this:

@employee = Employee.new(:client_id => params[:client_id])

Or if you have a given client, you can create the employee through this association and it will automatically assign the client_id

@employee = @client.employees.build # automatically assigns @employee.client_id

m.kruger wrote:

If I wanted a master-detail form (i.e. a listing of the top-level table of the several client companies and, upon selection, a detail listing of all employees of that company) how can I get one readily?

You can do something like this:

# in clients controller
def index
  @clients = Client.find(:all) # specify conditions how you like
end

def show
  @client = Client.find(params[:id])
end

# in clients/index.rhtml
<% for client in @clients %>
  <%= link_to client.name, :action => 'show', :id => client %>
<% end %>

# in clients/show.rhtml
Employees:
<% for employee in @client.employees %>
  <%= link_to employee.name, :controller => 'employees', :action => 'show', :id => employee %>
<% end %>


You could then create an Employees controller with a show action which basically mimics the Clients show action. You just display different information/relationships for that model.

Railscasts - Free Ruby on Rails Screencasts

Re: Foreign Key fields not addressed in views

ryanb;

I never expected such a thorough response! Thanks loads and I'll try it all now.