Rails:如何将数据迁移中的值链接到类/模型变量?

发布于 2024-07-23 10:43:41 字数 235 浏览 9 评论 0原文

我的 sql DB 包含表“jobs”和“job_categories”。

“job_categories”将工作类别字符串(即“软件开发”)与整数(即7)相关联。

我需要将这些关联保存到作业控制器中的变量中以用于各种查询功能。 如何使用 Rails 将 job_categories 表的更改动态链接到作业控制器中的变量? 我已经与 RoR 合作了几个星期,但对一切如何相互作用仍然有点模糊。 谢谢你!

my sql DB contains tables "jobs" and "job_categories."

"job_categories" associates job category strings (i.e. "Software Development") with an integer number (i.e. 7).

I need these associations saved into variables in my job controller for various query functions. How can I use rails to dynamically link changes to the job_categories table to variables in my jobs controller? I've worked with RoR for a few weeks now but am still a little fuzzy on how everything interacts. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦太阳 2024-07-30 10:43:41

您想要做的事情有一个大问题,但首先我会按要求回答您的问题。

在 JobsController 中创建类级访问器,然后在 JobCategory 类上编写一个观察者,在保存和销毁事件后对 JobsController 进行适当的更改。

class JobsController < ActionController::Base
  @@categories = JobCategory.find(:all)
  cattr_accessor :categories

  # ...
end

class JobCategoryObserver < ActiveRecord::Observer
  def after_save(category)
    JobsController.categories[category.name] = category.id
  end

  def after_destroy(category)
    JobsController.categories.delete(category.name)
  end
end

如果允许名称更改,您将需要额外的逻辑来删除旧名称。 ActiveRecord::Dirty 中的方法将对此有所帮助。

所以,陷阱。 这种方法的问题在于,通常您有多个处理请求的进程。 您可以对 job_categories 表进行更改,但该更改只会在一个进程中更新。 其他的现在都已经过时了。

您的 job_categories 表可能很小。 如果以任何频率访问它,它将被操作系统或数据库服务器缓存在内存中。 如果您查询得足够多,该查询的结果甚至可能被数据库缓存。 如果您不经常查询它,那么无论如何您都不应该费心尝试在 JobsController 内进行缓存。

如果您绝对必须在内存中缓存,那么最好使用 memcached。 然后,您将获得所有 Rails 进程都可以使用的单个缓存,并且没有过时的数据。

There's one big gotcha with what you're trying to do, but first I'll answer your question as asked.

Create class-level accessors in your JobsController, then write an Observer on the JobCategory class that makes the appropriate changes to the JobsController after save and destroy events.

class JobsController < ActionController::Base
  @@categories = JobCategory.find(:all)
  cattr_accessor :categories

  # ...
end

class JobCategoryObserver < ActiveRecord::Observer
  def after_save(category)
    JobsController.categories[category.name] = category.id
  end

  def after_destroy(category)
    JobsController.categories.delete(category.name)
  end
end

You'll need additional logic that removes the old name if you allow for name changes. The methods in ActiveRecord::Dirty will help with that.

So, the gotcha. The problem with an approach like this is that typically you have more than one process serving requests. You can make a change to the job_categories table, but that change only is updated in one process. The others are now stale.

Your job_categories table is likely to be small. If it's accessed with any frequency, it'll be cached in memory, either by the OS or the database server. If you query it enough, the results of that query may even be cached by the database. If you aren't querying it very often, then you shouldn't be bothering with trying to cache inside JobsController anyway.

If you absolutely must cache in memory, you're better off going with memcached. Then you get a single cache that all your Rails processes work against and no stale data.

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