2013-04-08 23 views
0

我:轨SQL查询

User id:integer name:integer 
class User 
has_many :complete_tasks 
end 

Task id:integer style:string uid:string 

CompleteTask id:integer style:string uid:string user_id:integer 
class CompleteTask 
belongs_to :user 
end 

我在DB

user = User.first 

ID一些记录:1名:田园

tasks = Tasks.all 

ID:1种类型=>“运行“uid =>”river“

id:2 style =>”jump“uid =>”sea“

ID:3风格=> “运行” 的uid => “海”

ID:4风格=> “运行” 的uid => “河”

ID:5风格=>“运行“UID =>” 森林 “

user.complete_tasks.all 

ID:1种类型=> ”运行“ 的uid => ”河“ USER_ID => 1

ID:2样式=> ”跳“ 的uid =>”海“user_id => 1

如何从Task中获取记录,其中字段:style和:uid在模型CompleteTask中不是等效字段:style和:uid。

+0

有什么用'id3'和任务之间的区别'id5'相比其他的呢? – Zippie 2013-04-08 18:43:57

+0

我想获取所有记录任务,其中字段:style和:uid与模型CompleteTasks中的字段:style和:uid不一致。 – user2232650 2013-04-08 19:00:20

+0

我唯一想做的事情就是制作两个/ for循环,然后比较它。这是一个非常差的数据库模型 – Zippie 2013-04-08 19:31:07

回答

0

这是一个贫穷的数据库模型,我会建议使用别的东西。也许是像task模型那样的布尔值或sm​​thng,因此您可以检查它是否完整。 不管怎么说,这里是一个解决方案:

all_tasks = Task.all 
complete_tasks = CompleteTask.all 

complete_tasks.each do |complete_task| 
    #find if there is a task with the corresponding uid and style in the complete tasks 
    tasks = all_tasks.where(:uid => complete_task.uid, :style => complete_task.style) 
    #remove it from all_tasks (all_tasks means tasks that haven't finished yet) 
    all_tasks = @all_tasks - tasks 
end 

return all_tasks 
+0

谢谢,我会考虑如何改变。 – user2232650 2013-04-08 19:47:29

+0

没问题,如果你写下你如何执行一切,也许我可以帮你 – Zippie 2013-04-08 19:48:33

0

如果我正确地理解了你,你试图找到所有没有与完成任务相同的ID的任务。

对于您应该使用find condition --something,如:

Task.find(:all, :conditions => { :complete => false });