Ruby on Rails 3:将额外数据与模型关联
几乎是 Ruby/Rails 的初学者。
我有一个包含许多部分的视频:
@video = Video.find(params[:id])
@[email protected];
我想将颜色属性与每个部分相关联,但颜色是在控制器中计算的,而不是存储在数据库中。
到目前为止,我只是在控制器中创建了一个 @colours
数组,其中索引与该部分的索引相匹配
hues = [41, 6, 189, 117, 279]
saturation = 100;
brightness = 45;
@colours = [];
@sections.each { @colours.push Color::HSL.new(hues[j%hues.length], saturation, brightness) }
,以便 @sections[i]
对应于 @colors[i]
。
这工作得很好,但似乎不是最好的方法。我想扩展我的部分模型,使其具有“颜色”属性,以便我可以通过执行 @sections[i].colour
来访问它
我尝试将其放入 models/sectiondata.rb :
class SectionData
extend Section
attr_accessor :colour
end
但是当我尝试在控制器中执行 SectionData.new
时,我收到一条错误消息,指出找不到该类。我也不知道如何让原始的 @section
成为新的 SectionData 类的一部分。
解决这个问题的最佳方法是什么?任何有关我的编码风格的建议也将不胜感激,Ruby 与我习惯的风格相差很大。
Pretty much a total beginner to Ruby/Rails.
I have a Video with many Sections:
@video = Video.find(params[:id])
@[email protected];
I want to associate a colour attribute with each Section, but the colour is calculated in the controller, rather than stored in the database.
So far I have been simply creating a @colours
array in my controller where the index matched up with the index of the section
hues = [41, 6, 189, 117, 279]
saturation = 100;
brightness = 45;
@colours = [];
@sections.each { @colours.push Color::HSL.new(hues[j%hues.length], saturation, brightness) }
so that @sections[i]
corresponds to @colours[i]
.
This works fine, but doesn't seem like the best approach. I would like to extend my Sections model so that it has a 'colour' attribute, so that I could access it by doing @sections[i].colour
I tried putting this in models/sectiondata.rb :
class SectionData
extend Section
attr_accessor :colour
end
but when I try to do SectionData.new
in my Controller I get an error saying it can't find the class. I also don't know how I would get the original @section
to be part of the new SectionData class.
What is the best way to approach this problem? Any tips on my coding style would also be appreciated, Ruby is a big step away from what I'm used to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为在这种情况下实现 哈希 比使用两个数组更好相互对应。
例如,
I think its a better idea to implement hashes in this situation instead of having two arrays corresponding to each other.
For example,
首先,我尝试将它们作为 attr_accessors 放入
Section
模型中,但不要这样做。糟糕的编码员!如果 ActiveRecord 再次查询数据库以加载该页面,即使查询已被缓存,它们也不会显示。如果您需要 ActiveRecord 方法,您可以使用 ActiveModel相反,我为属性访问器创建了一个类:
sectiondata.rb:
然后您可以使用将 Section 对象映射到 SectionData 的对象来使用它们:
< strong>xxx_controller.rb
然后,给定一个名为“section”的Section对象,您可以使用
@sectionDatas[section]
访问它,这将适用于发生在页的First I tried putting them as attr_accessors in the
Section
model, don't do that. Bad coder! They will not show up if ActiveRecord queries the database again for that page load, even if the query is cached. If you need the ActiveRecord methods you can use ActiveModelInstead, I created a class for the attribute accessors:
sectiondata.rb:
Then you can work with them using an object which maps a Section object to SectionData:
xxx_controller.rb
Then, given a Section object called 'section', you could access it using
@sectionDatas[section]
, and this will work for any database queries that occur in the page of