0

我有:

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

我在数据库中有一些记录

user = User.first

id:1 名称:Den

tasks = Tasks.all

id:1 风格 => “奔跑” uid => “河流”

id:2 风格 => “跳跃” uid => “海”

id:3 风格 => “跑” uid =>“海”

id:4 风格 => “跑” uid => “河”

id:5 风格 => "运行" uid =>"森林"

user.complete_tasks.all

id:1 style => "run" uid => "river" user_id => 1

id:2 style => "jump" uid => "sea" user_id => 1

如何从任务中获取记录,其中字段:样式和:uid 一起不是模型 CompleteTask 中的等效字段:样式和:uid。

4

2 回答 2

0

如果我对您的理解正确,那么您正在尝试查找与完整任务具有不同 ID 的所有任务。

为此,您应该使用查找条件——类似于:

Task.find(:all, :conditions => { :complete => false });
于 2013-04-08T18:53:02.203 回答
0

这是一个糟糕的数据库模型,我建议使用其他东西。可能是模型中的布尔值或类似的东西,task因此您可以检查它是否完整。无论如何,这是一个解决方案:

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
于 2013-04-08T19:38:21.747 回答