Ok my system model has_many prices, and rails nicely lets me do this <% for price in @systems[counter].prices %> <tr> <td bgcolor=#FDFDFD><a href="#" class=content_storename1><%= price.store_name %></a></td> <td align=right bgcolor=#FDFDFD width=50><a href="#" class=content_price1>...<%= price.price %></td> </tr> <% end %> this does as expected and lists all the prices for a system, is there an easy way to say only get the first 3 prices? Im a bit stumped ive tried @systems[counter].prices[1] to get just the first one and no go. Can anyone help? Thanks Sam _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sam Mayes wrote:> this does as expected and lists all the prices for a system, is > there an easy way to say only get the first 3 prices? Im a bit stumpedI''d change the relational mapping to get only the records you want, so instead of: has_many :prices try something like: has_many :recent_prices, :order => "id DESC", :limit => 3, :class_name => "Price" You can substitute :top_prices for whatever you like... recent_prices, highest_prices, lowest_prices, newest_prices, whatever. Then you just address the association like this: <% for price in @systems[counter].recent_prices %> ... <% end %> See http://api.rubyonrails.com/classes/ActiveRecord/Associations/ ClassMethods.html for more info –– it''s worth the read. Regards, --- Justin French Indent.com.au TextDrive.com
Sam, I''m still learning rails, but I think this may help... to get the first price: Prices.find(:first, :conditions => ["system_id = ?",@systems[counter].id]) to get the first three Prices.find(:first, :conditions => ["system_id = ?",@systems[counter].id], :limit => 3) Of course, this depends on your model/class name and table structure... -- Andrew Stone _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks justin, Have the book forgot to search more in the api. sam On 9/4/05, Andrew Stone <stonelists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Sam, > > I''m still learning rails, but I think this may help... > > to get the first price: > > Prices.find(:first, :conditions => ["system_id = ?",@systems[counter].id]) > > to get the first three > > Prices.find(:first, :conditions => ["system_id = ?",@systems[counter].id], > :limit => 3) > > Of course, this depends on your model/class name and table structure... > > -- > Andrew Stone > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I have the Human class. In the humans table i have the fields mother_id and father_id I would like to define in human.rb that: has_many :children, :class_name=>''Human'' but I can''t figure out how to specify the foreign_keys. Initially I was using f_children and m_children, and added them to obtain the children, but I wonder if it''s a way to get the children directly. Bogdan _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I''ve managed to get them using :finder_sql Is there any other way? not that I would not be satisfied with this one _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Bogdan Ionescu wrote:> I have the Human class. > In the humans table i have the fields mother_id and father_id > I would like to define in human.rb that: > > has_many :children, :class_name=>''Human'' > > but I can''t figure out how to specify the foreign_keys. Initially I was > using f_children and m_children, and added them to obtain the children, but > I wonder if it''s a way to get the children directly.Surely you want: class Human < ActiveRecord::Base end class Mother < Human has_many :children, :class_name => ''Human'', :foreign_key => ''mother_id'' end class Father < Human has_many :children, :class_name => ''Human'', :foreign_key => ''father_id'' end and have a ''type'' field in the Human table? Except under rather unusual circumstances, a Human shouldn''t be capable of being both a Father and a Mother, after all... By the way, does anyone else have any smart ideas as to how to achieve this with acts_as_tree? -- Alex