Rails 命名约定存在问题

发布于 2024-12-21 19:44:02 字数 1917 浏览 2 评论 0原文

我想我在我的应用程序中遵循了 Rails 命名约定。但是当我在终端测试代码时,我遇到了一些违反命名约定的错误。这是我的终端会话:

irb(main):010:0> a = _
=> #<Neighborhood id: 24, name: "Lincoln Park", created_at: "2011-12-03 20:29:00", updated_at: "2011-12-03 21:08:47", minlat: 41.91092, maxlat: 41.925658, minlng: -87.648761, maxlng: -87.636117>
irb(main):011:0> a.cta_trains
NoMethodError: undefined method `cta_trains' for #<Neighborhood:0x007fd666ee61e8>
    from /usr/local/Cellar/ruby/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'

现在,当我尝试 a.CtaTrains 时:

irb(main):012:0> a.CtaTrains
  CtaTrain Load (0.4ms)  SELECT "cta_trains".* FROM "cta_trains" INNER JOIN "cta_locations" ON "cta_trains"."id" = "cta_locations"."CtaTrain_id" WHERE "cta_locations"."neighborhood_id" = 24
SQLite3::SQLException: no such column: cta_locations.CtaTrain_id: SELECT "cta_trains".* FROM "cta_trains" INNER JOIN "cta_locations" ON "cta_trains"."id" = "cta_locations"."CtaTrain_id" WHERE "cta_locations"."neighborhood_id" = 24
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: cta_locations.CtaTrain_id: SELECT "cta_trains".* FROM "cta_trains" INNER JOIN "cta_locations" ON "cta_trains"."id" = "cta_locations"."CtaTrain_id" WHERE "cta_locations"."neighborhood_id" = 24

从我的模型来看:

class Neighborhood < ActiveRecord::Base

  has_many :cta_trains, :through => :cta_locations
  has_many :cta_locations, :foreign_key => :neighborhood_id

end 

class CtaTrain < ActiveRecord::Base

  has_many :neighborhoods, :through => :cta_locations
  has_many :cta_locations, :foreign_key => :cta_train_id

end

class CtaLocation < ActiveRecord::Base

  belongs_to :neighborhood
belongs_to :cta_train

end

我处于停滞状态、卡住、用头撞墙等。任何帮助都会很棒。

Rails 菜鸟在这里......好像这一点并不明显......

I think I have followed the rails naming conventions in my app. But when I am in the terminal testing code I am coming across some errors that go against naming conventions. Here is my terminal session:

irb(main):010:0> a = _
=> #<Neighborhood id: 24, name: "Lincoln Park", created_at: "2011-12-03 20:29:00", updated_at: "2011-12-03 21:08:47", minlat: 41.91092, maxlat: 41.925658, minlng: -87.648761, maxlng: -87.636117>
irb(main):011:0> a.cta_trains
NoMethodError: undefined method `cta_trains' for #<Neighborhood:0x007fd666ee61e8>
    from /usr/local/Cellar/ruby/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activemodel-3.1.1/lib/active_model/attribute_methods.rb:385:in `method_missing'

Now when I try a.CtaTrains:

irb(main):012:0> a.CtaTrains
  CtaTrain Load (0.4ms)  SELECT "cta_trains".* FROM "cta_trains" INNER JOIN "cta_locations" ON "cta_trains"."id" = "cta_locations"."CtaTrain_id" WHERE "cta_locations"."neighborhood_id" = 24
SQLite3::SQLException: no such column: cta_locations.CtaTrain_id: SELECT "cta_trains".* FROM "cta_trains" INNER JOIN "cta_locations" ON "cta_trains"."id" = "cta_locations"."CtaTrain_id" WHERE "cta_locations"."neighborhood_id" = 24
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: cta_locations.CtaTrain_id: SELECT "cta_trains".* FROM "cta_trains" INNER JOIN "cta_locations" ON "cta_trains"."id" = "cta_locations"."CtaTrain_id" WHERE "cta_locations"."neighborhood_id" = 24

From my models:

class Neighborhood < ActiveRecord::Base

  has_many :cta_trains, :through => :cta_locations
  has_many :cta_locations, :foreign_key => :neighborhood_id

end 

class CtaTrain < ActiveRecord::Base

  has_many :neighborhoods, :through => :cta_locations
  has_many :cta_locations, :foreign_key => :cta_train_id

end

class CtaLocation < ActiveRecord::Base

  belongs_to :neighborhood
belongs_to :cta_train

end

I am at a standstill, stuck, banging my head against the wall, etc. Any help would be fabulous.

Rails noobie here....as if this point is not obvious.....

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

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

发布评论

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

评论(2

坏尐絯℡ 2024-12-28 19:44:02

注意到您似乎在 IRB 中...相反,在处理活动记录类时,我会尝试留在 Rails 控制台中。

所以从

bundle exec rails console

Noticed that you appear to be in IRB... Instead, I'd try to stay in the rails console when working with your active-record classes.

so start that with

bundle exec rails console
往日 2024-12-28 19:44:02

这里你需要的是一个连接表。请参阅关联has_and_belongs_to_many

连接表将存储某个Neighbourhood和某个CtaTrain之间的链接。在这里,它是 CtaLocation,但如果您不打算实际使用这个模型,您甚至可以不定义它。

例如,您可以使用三个表(neighbourhoods、cta_trains 和 cta_trains_neighbourhoods)和两个模型来实现它,例如:

class Neighbourhood
  has_and_belongs_to_many :cta_trains
end

class CtaTrain
  has_and_belongs_to_many :neighbourhoods
end

What you need here is a junction table. See the association has_and_belongs_to_many.

The junction table will store the links between a certain Neighbourhood and a certain CtaTrain. Here, it's CtaLocation but if you don't plan to actually use this model, you could even not define it.

For instance, you can achieve it With three tables (neighbourhoods, cta_trains and cta_trains_neighbourhoods) and only two models like :

class Neighbourhood
  has_and_belongs_to_many :cta_trains
end

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