开发者

Find first two records from collection

开发者 https://www.devze.com 2023-01-18 18:46 出处:网络
I want to retrieve the first two records from collection such that, collection is like: @collect_firstnames = @name.firstnames

I want to retrieve the first two records from collection such that, collection is like:

@collect_firstnames = @name.firstnames

From this collection I want to fetch first two records...

I used开发者_开发百科 limit it is not working, :limit => 2


I take it that @name.firstnames is a has_many relationship. You need to pass the options to all:

@name.firstnames.all(:limit => 2)


You can use the :limit option like this:

@first_two = Name.find(:all, :limit => 2)

Edit

It's the same idea, assuming firstnames is a relationship (just like Swanand said below)

@first_two = @name.firstnames.all(:limit => 2)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号