通过嵌套命名空间引用具有belongs_to关系的模型中的属性

发布于 2024-10-10 11:32:18 字数 1570 浏览 3 评论 0原文

好吧,所以我以为我理解关系规范如何在 Rails 中工作,但我已经为此苦苦挣扎了一天。

在某些情况下,我有两个模型“汽车”和“汽车”。模型名称(例如 Impala、Charger 等),其中 Cars 是模型名称的实例,而模型名称实际上只不过是模型名称和一些其他模型级别属性的查找表。模型名称控制器嵌套在管理命名空间内,因为只有管理员可以 CRUD 模型名称。最终用户可以将汽车实例添加到汽车模型中。

因此,在routes.rb中我有:

  resources :cars

  namespace :admin do resources :model_names end

模型的定义为:

class Admin::ModelName < ActiveRecord::Base
end
class Car < ActiveRecord::Base
  belongs_to :admin_model_name
end

迁移是:

class CreateCars < ActiveRecord::Migration
  def self.up
    create_table :cars do |t|
      t.string :chassis_number
      t.string :description
      t.references :admin_model_name
      t.timestamps
   end
end

class CreateAdminModelNames < ActiveRecord::Migration
  def self.up
    create_table :admin_model_names do |t|
      t.string :model
      t.integer :sort_index
      #...additional attributes removed
      t.timestamps
    end

ModelName 的管理 CRUD 都工作得很好。问题出在汽车视图中。我认为我应该引用特定的汽车型号名称,如下所示:

<%= @car.admin_model_names.Model =>

但我收到错误:

undefined method `admin_model_names' for #<Car:0x000001040e2478>

我已在 ModelNames 模型上尝试了 attr_accessible 但无济于事。正确引用了基础数据。我在汽车和汽车之间也有 HABTMT 关系。用户和所有这些都可以很好地引用来自不同实体视图的彼此属性。但一直没能让它发挥作用。是否是由于嵌套资源&管理控制继承?

我发现唯一有效的引用方法是:

 <%= Admin::ModelName.find(@car.admin_model_name_id).model %>

但这似乎确实需要太多代码(以及查找费用)来获取该属性。有Rails方式吗?

提前致谢。

斯科特

Ok, so I thought I understood how the relationship specifications work in rails but I've been struggling with this for a day now.

Some context, I have two models Cars & Model Names (e.g. Impala, Charger, etc), where Cars are instances of Model Names, and Model Names really is nothing more than a lookup table of Model Names and some other model level attributes. The Model Name controller is nested within the admin namespace as only admins can CRUD Model Names. End users can add instances of cars to the Cars model.

So, in routes.rb I have:

  resources :cars

  namespace :admin do resources :model_names end

The Model's are defined as:

class Admin::ModelName < ActiveRecord::Base
end
class Car < ActiveRecord::Base
  belongs_to :admin_model_name
end

The Migrations are:

class CreateCars < ActiveRecord::Migration
  def self.up
    create_table :cars do |t|
      t.string :chassis_number
      t.string :description
      t.references :admin_model_name
      t.timestamps
   end
end

class CreateAdminModelNames < ActiveRecord::Migration
  def self.up
    create_table :admin_model_names do |t|
      t.string :model
      t.integer :sort_index
      #...additional attributes removed
      t.timestamps
    end

The admin CRUD of ModelName all work great. The problem is in the Car views. I think I should be referencing a particular cars model name like such:

<%= @car.admin_model_names.Model =>

But I get the error:

undefined method `admin_model_names' for #<Car:0x000001040e2478>

I've tried the attr_accessible on the ModelNames model but to no avail. The underlying data is referenced correctly. I have also have HABTMT relationship between Cars & Users and that all worked fine referencing each others attributes from the different entities views. But haven't been able to get this to work. Is it due to the nested resource & admin control inheritance?

The only referencing method I found that works is:

 <%= Admin::ModelName.find(@car.admin_model_name_id).model %>

But this really seems to be too much code (and expense of a find) to get to that attribute. Is there a Rails way?

Thanks in advance.

Scott

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

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

发布评论

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

评论(2

[旋木] 2024-10-17 11:32:18

您是否尝试过:

class Car < ActiveRecord::Base   
  belongs_to :admin_model_name, :class_name => "Admin::ModelName" 
end

http://guides.rubyonrails.org/association_basics.html

第 3.4 节所述?

您可能还需要设置

:foreign_key => "admin_model_name_id"

attribute to specify the referencing model.

希望有帮助。

Have you tried:

class Car < ActiveRecord::Base   
  belongs_to :admin_model_name, :class_name => "Admin::ModelName" 
end

as stated in

http://guides.rubyonrails.org/association_basics.html

section 3.4?

you may also need to set the

:foreign_key => "admin_model_name_id"

attribute to specify the referencing model.

Hope it helps.

半边脸i 2024-10-17 11:32:18

您是否尝试过

class Car < ActiveRecord::Base
  belongs_to :admin_model_name, :class_name => 'Admin::ModelName'
end

并在必要时添加:foreign_key => ”并将此列添加到您的 cars 表中。

Did you try

class Car < ActiveRecord::Base
  belongs_to :admin_model_name, :class_name => 'Admin::ModelName'
end

and if necessary add :foreign_key => '' and add this column to your cars table.

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