One controller, multiple models, how to create?

Hi, guys,
I want to create a controller that assocates multiple
models(ActiveRecord),
how to building ?
For example, my controller called posts, it has the models post ,
catalog
and tag.

thanks all.

One controller, multiple models, how to create ?

class PostsController < ApplicationController
def new
p = Post.new
c = Catalog.new
t = Tag.new
end
end

class Post < ActiveRecord::Base
end

class Catalog < ActiveRecord::Base
end

class Tag < ActiveRecored::Base
end

Put all of model class in one file of the post model ?

2011/8/3 7stud – [email protected]

As far as I know, models aren’t associated with a controller. So you
could just ‘rails generate model ModelName field:type’ three times for
the Post, Catalog, and Tag models. Then in some controller, you can
write:

class PostsController < ApplicationController
def new
@title = “Some page”
@val = 10

@post = Post.new
@post.email = "[email protected]"

@catalog = Catalog.new
@catalog.name = "Pottery Barn"

@tag = Tag.new
@tag.name = "dishes"

end

end

And then in view, like new.html.erb, you can write:

Posts#new

Post model: <%= @post.email %>
Catalog model: <%= @catalog.name %>
Tag model: <%= @tag.name %>
Val: <%= @val %>

which is inserted in the layout: application.html.erb,

<%= @title %> <%= csrf_meta_tag %>

<%= yield %>