开发者

Rails order based on association

开发者 https://www.devze.com 2023-01-23 20:31 出处:网络
I have a Problem and a Solution model. Problem has many solutions and Solution belongs to problem. I need to get the recently solved problem, that means, get the last X solutions an开发者_运维问答d g

I have a Problem and a Solution model. Problem has many solutions and Solution belongs to problem.

I need to get the recently solved problem, that means, get the last X solutions an开发者_运维问答d get it's problem. I'm pretty sure there's a way to do this with named_scopes but I can't figure it out.

Any help is really appreciated :-)

Thanks,

Nicolás Hock I.


I actually got the named_scope working:

named_scope :solved_recently, :joins => :solutions, :order => "solutions.created_at DESC"
# Problem.solved_recently

Thanks anyway @Jacob :)


I think this should work:

x = #some number
solutions = Solution.find(:all, :order => ['created_at desc'], :limit => x)
problems  = solutions.map {|solution|
   solution.problem
}
0

精彩评论

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