我是Ruby on Rails的新手。
我有两个模型。
class Experience < ActiveRecord::Base
has_and_belongs_to_many: assets
end
class Assets < ActiveRecord::Base
has_and_belongs_to_many: experiences
end在我的体验控制器中,我尝试获取与体验关联的资产集合。(父子)
我目前正在使用will_paginate gem,并试图为此构建一个集合,以传递集合参数。
will_paginate(collection, options)我该怎么做呢?
发布于 2016-03-11 11:12:16
你的模型关系应该是:
class Experience < ActiveRecord::Base
has_many: assets
end
class Assets < ActiveRecord::Base
belongs_to: experience
end在Experience Controller中,获取相关资源
# single experience data
@experience = Experience.first
@experience.assets
# multiple experiences data
@experiences = Experience.all.includes(:assets)
# you can also use this in view
@experiences.each do |x|
x.assets.each do |a|
a.title (ex. field name)
...
end
endhttps://stackoverflow.com/questions/35915401
复制相似问题