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.
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 ?
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 = "xxx@yahoo.com"
@catalog = Catalog.new @catalog.name = "Pottery Barn"
@tag = Tag.new @tag.name = "dishes" end
end
And then in new.html.erb, you can write:
<h1>Posts#new</h1>
<div>Post model: <%= @post.email %></div> <div>Catalog model: <%= @catalog.name %></div> <div>Tag model: <%= @tag.name %></div> <div>Val: <%= @val %></div>