Rails命名约定问题

Rails命名约定问题

本文介绍了Rails命名约定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我在我的应用程序中遵循了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

我处于停滞状态,被卡住,把头撞在墙上,等等.任何帮助都是很棒的.

将noobie推到这里....好像这一点并不明显.....

解决方案

注意到您似乎在IRB中……相反,在使用活动记录类时,我会尝试停留在rails控制台中. /p>

所以从

开始

bundle exec rails console

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.....

解决方案

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

这篇关于Rails命名约定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 03:20