spu*_*ity 2 ruby loops ruby-on-rails
我想做这样的事情:
@profiles
#(I think in Java, so I have declared an @profiles variable there! I know it's probably wrong!)
@users.each do |user|
profile = Profile.find(params[user.id])
@profiles.add(profile)
end
Run Code Online (Sandbox Code Playgroud)
用户和个人资料具有一对一的关系.
用户有一个个人资料,个人资料属于用户
您需要初始化数组.
@profiles = []
@users.each do |user|
profile = Profile.find(params[user.id])
@profiles << profile if profile
end
Run Code Online (Sandbox Code Playgroud)
如果你有关系,你应该只能说:
@profiles = []
@users.each do |user|
profile = user.profile
@profiles << profile if profile
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2168 次 |
| 最近记录: |