Ruby on Rails 3:将额外数据与模型关联

发布于 2024-10-24 17:41:04 字数 1062 浏览 2 评论 0原文

几乎是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你与昨日 2024-10-31 17:41:04

我认为在这种情况下实现 哈希 比使用两个数组更好相互对应。
例如,

result_hash = {sports_section => 'blue'}

I think its a better idea to implement hashes in this situation instead of having two arrays corresponding to each other.
For example,

result_hash = {sports_section => 'blue'}
懒猫 2024-10-31 17:41:04

首先,我尝试将它们作为 attr_accessors 放入 Section 模型中,但不要这样做。糟糕的编码员!如果 ActiveRecord 再次查询数据库以加载该页面,即使查询已被缓存,它们也不会显示。如果您需要 ActiveRecord 方法,您可以使用 ActiveModel

相反,我为属性访问器创建了一个类:

sectiondata.rb:

class SectionData
    attr_accessor :left, :width, :timeOffset, :colour, :borderColour
end

然后您可以使用将 Section 对象映射到 SectionData 的对象来使用它们:

< strong>xxx_controller.rb

@video = Video.find(params[:id])
@[email protected]

require 'sectiondata.rb'
@sectionDatas=Hash.new

@sections.each do |i|
  sd=SectionData.new

  # set whatever attributes here, e.g.
  sd.left= total/multiplier

  @sectionDatas[i]=sd
end

然后,给定一个名为“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 ActiveModel

Instead, I created a class for the attribute accessors:

sectiondata.rb:

class SectionData
    attr_accessor :left, :width, :timeOffset, :colour, :borderColour
end

Then you can work with them using an object which maps a Section object to SectionData:

xxx_controller.rb

@video = Video.find(params[:id])
@[email protected]

require 'sectiondata.rb'
@sectionDatas=Hash.new

@sections.each do |i|
  sd=SectionData.new

  # set whatever attributes here, e.g.
  sd.left= total/multiplier

  @sectionDatas[i]=sd
end

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文