ktulasi
Joined: 21 Oct 2009 Posts: 55
|
Posted: Mon Feb 22, 2010 12:51 pm Post subject: Relation between two models using hasmany |
|
|
The relation between two models can be taken in many ways,
one of the way can be hasmany relation.
The has many relation for the models can be given as,
One, quotes model having relation to lines model as,
class Quote < ActiveRecord::Base
has_many:lines
end
class Line < ActiveRecord::Base
belongs_to :quote, :class_name => "Quote", :foreign_key => "quote_id"
here we have specified a classname i.e; Quote and a foreign key as quote_id and quote as a method
like this we can give a relation.
now,in a controller we can use like to get the data from quotes table
@var = Quote.lines
and we can use like the way to get the data from Line table
@var = Line.quote |
|