English 中文(简体)
B. 如何在铁路中应用两种不同模式
原标题:How to apply pagination on two different models in rails 3

我想对两个表格进行解释。

宣传

Id    Message         Created at
1     Message One     2012-04-23 13:11:23
2     Message Two     2012-04-23 13:11:26
3     Message Three   2012-04-23 13:11:27

投标

Id    Comment         Created at
1     Comment One     2012-04-23 13:11:24
2     Comment Two     2012-04-23 13:11:25
3     Comment Three   2012-04-23 13:11:28

I want following result (Two latest 投标 or 宣传) using one query

   Id    Content         Created at
    3     Comment Three   2012-04-23 13:11:28
    3     Message Three   2012-04-23 13:11:27
问题回答

我不妨问,为什么你们需要两个单独的电文和标语表。 对我来说,两者都像它们储存某种形式的文本通知一样。 我猜测,你应当使用单一表格的“遗产”或多形态协会(如果是,今后还会有其他商业逻辑)。

请登录,以便了解更多情况。

我认为,你在两张桌子上座的这一要求是设计书。 我没有完整的背景,我不敢肯定,你现在是否有改变设计的自由。 但是,只是一个想法!

首先,从多种表格中抽取记录,是一种密码。

然而,如果你想采用这一《发展协议》办法,那么,根据守则,你可能会有帮助:

records = [Message, Comment, Post].inject([]) do |record, with_class|
  record+with_class.order( created_at DESC )#you can add limit, offset here
end


#Now paginate those records

@records = WillPaginate::Collection.create(current_page, per_page, records.size) do         |pager|
            pager.replace(@records)
           end




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签