Topic: Playing with the model

Is it possible to work with data at an abstract level in your methods?

Ie: have an object passed in and itterate through all it's variables to display them in a table. (Get the Model for TableA with columns A,B,C and display them in a table, and can do the same for TableB with columns P,Q,R,S.)

Also, is there a way to generate a model that would work with the data returned by a query like:

SELECT c.ID, c.name, a.address
FROM Companies c, Addresses a
WHERE a.company_id = c.id
AND a.type = "main"

Re: Playing with the model

KittyKate wrote:

Is it possible to work with data at an abstract level in your methods?

Ie: have an object passed in and itterate through all it's variables to display them in a table. (Get the Model for TableA with columns A,B,C and display them in a table, and can do the same for TableB with columns P,Q,R,S.)

You can call "attributes" on a given model to loop through all of its columns/attributes:

@person.attributes

Scaffolding offers another way to do this, generate a scaffolding and check it out.

KittyKate wrote:

Also, is there a way to generate a model that would work with the data returned by a query like:

SELECT c.ID, c.name, a.address
FROM Companies c, Addresses a
WHERE a.company_id = c.id
AND a.type = "main"

You can call find_by_sql on Company. Is there a reason you aren't doing this with the find method though?

@company = Company.find(:all, :include => :addresses)
@company.main_address.address

You can then create the "main_address" method in the company model to fetch the correct one. This feels like a better solution to me.

Railscasts - Free Ruby on Rails Screencasts