i have a comment Model. it has already “belongs_to :poem” entry.
is there any way to use this model to another controller (eg:-
belongs_to :post) . if it is possible then how do i do the migration
and other stuffs please explain a bit.
nirosh
i have a comment Model. it has already “belongs_to :poem” entry.
is there any way to use this model to another controller (eg:-
belongs_to :post) . if it is possible then how do i do the migration
and other stuffs please explain a bit.
nirosh
I don’t think controllers have anything to do with your question. A
controller is a program that receives requests from the browser and
processes them, most likely involving models, and then sends a
response back to the browser.
What you are talking about, I think, is about activerecord
associations. You can make as many associations between models
(tables) as you wish. A comment can belong to a poem and also to a
post:
class Comment < ActiveRecord::Base
belongs_to :poem
belongs_to :post
end
class Poem < ActiveRecord::Base
has_many :comments
end
class Post < ActiveRecord::Base
has_many :comments
end
Hi nirosh,
belongs_to associations represent other models/tables not controllers.
You can have as many different Controllers as you like, created
through migrations or manually - they are simply the way of ‘getting
in’ to your application. All the heavy lifting should be done in the
models.
You can set your models up as so:
comment
has_many: poems
has_many: posts
poem
belongs_to: comment
post
belongs_to: comment
Your poem and post tables would then each have a column/attribute:
comment_id
Each poem and each post could then ‘attach’ themselves to the same
comment_id so that each single comment could be associated with both a
poem and a post.
You create your post migration the same way you created your poem
migration, apart from any additional attributes specifically related
to post.
HTH
Paul
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs