has_many :通过不授予对基类的_id 的访问权限

发布于 2024-11-09 08:02:09 字数 2195 浏览 0 评论 0原文

模型代码

class Location < ActiveRecord::Base
  attr_accessible :day_tokens, :name, :address, :phone
  has_many :locavailability
  has_many  :dayavailables, :through => :locavailability
  attr_reader :day_tokens
  def day_tokens=(ids)
    self.locavailability.dayavailable_id= ids.split(",")
  end
end

class Dayavailable < ActiveRecord::Base
  has_many  :locavailability
  has_many  :locations, :through => :locavailability
end

class Locavailability < ActiveRecord::Base
  attr_accessible :dayavailable_id, :location_id
  belongs_to :dayavailable
  belongs_to :location
end

数据库列:

locations: id, name, address, phone, hours, created_at, updated_at
dayavailables: id, day, time, created_at, updated_at
locavailabilities: id, location_id, dayavailable_id, created_at, updated_at

错误

irb(main):004:0> l = Location.find(1)
=> #<Location id: 1, name: "test", address: "123 Fake Street", phone: "23443234324", hours: "M-F 9 - 5", created_at: "2011-05-16 04:56:31", updated_at: "2011-05-16 04:56:31">
irb(main):005:0> l.class
=> Location(id: integer, name: string, address: string, phone: string, hours: string, dentistry_id: integer, created_at: datetime, updated_at: datetime)
irb(main):006:0> l.locavailability
=> []
irb(main):007:0> l.dayavailable_id
NoMethodError: undefined method `dayavailable_id' for #<Location:0xb6af7c40>
    from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.0/lib/active_model/attribute_methods.rb:364:in `method_missing'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/attribute_methods.rb:46:in `method_missing'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/attribute_methods.rb:44:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/attribute_methods.rb:44:in `method_missing'
    from (irb):6

位置类不应该访问dayavailable_id吗?我将使用 Railscasts 第 258 集 github

我错过了什么?

Model code

class Location < ActiveRecord::Base
  attr_accessible :day_tokens, :name, :address, :phone
  has_many :locavailability
  has_many  :dayavailables, :through => :locavailability
  attr_reader :day_tokens
  def day_tokens=(ids)
    self.locavailability.dayavailable_id= ids.split(",")
  end
end

class Dayavailable < ActiveRecord::Base
  has_many  :locavailability
  has_many  :locations, :through => :locavailability
end

class Locavailability < ActiveRecord::Base
  attr_accessible :dayavailable_id, :location_id
  belongs_to :dayavailable
  belongs_to :location
end

Database Columns:

locations: id, name, address, phone, hours, created_at, updated_at
dayavailables: id, day, time, created_at, updated_at
locavailabilities: id, location_id, dayavailable_id, created_at, updated_at

Error

irb(main):004:0> l = Location.find(1)
=> #<Location id: 1, name: "test", address: "123 Fake Street", phone: "23443234324", hours: "M-F 9 - 5", created_at: "2011-05-16 04:56:31", updated_at: "2011-05-16 04:56:31">
irb(main):005:0> l.class
=> Location(id: integer, name: string, address: string, phone: string, hours: string, dentistry_id: integer, created_at: datetime, updated_at: datetime)
irb(main):006:0> l.locavailability
=> []
irb(main):007:0> l.dayavailable_id
NoMethodError: undefined method `dayavailable_id' for #<Location:0xb6af7c40>
    from /usr/lib/ruby/gems/1.8/gems/activemodel-3.0.0/lib/active_model/attribute_methods.rb:364:in `method_missing'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/attribute_methods.rb:46:in `method_missing'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/attribute_methods.rb:44:in `send'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.0/lib/active_record/attribute_methods.rb:44:in `method_missing'
    from (irb):6

Shouldn't Location class have access to dayavailable_id?? I am going by the code in railscasts episode 258 on github

What am I missing?

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

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

发布评论

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

评论(1

红玫瑰 2024-11-16 08:02:09

为什么 Location 应该有权访问 dayavailable_id
如果您给出 true,则 dayavailable_idlocavailability 列:

数据库列:

位置:ID、姓名、地址、电话、营业时间、创建时间、更新时间

dayavailables:id、日期、时间、created_at、updated_at

可用地点:id、location_id、dayavailable_id、created_at、updated_at

试试这个:
更新

l = Location.find(1)
l.dayavailable_ids #=> Returns an array of the associated objects’ ids

但是如果你调用dayavailable_id,它应该是表的一列。

所以 l.dayavailable_ids 将执行此数据库查询:

select id from dayavailables d
inner join locavailabilities l on l.dayavailable_id = d.id 
where l.location_id = 1 

但 Locavailability.find(1).dayavailable_id 将执行此数据库查询(不是真的,只是为了更具可读性):

select dayavailable_id from locavailabilities l
where l.location_id = 1 

Why Location should have access to dayavailable_id?
The dayavailable_id is column of locavailabilities, if you given true:

Database Columns:

locations: id, name, address, phone, hours, created_at, updated_at

dayavailables: id, day, time, created_at, updated_at

locavailabilities: id, location_id, dayavailable_id, created_at, updated_at

Try this:
UPDATED

l = Location.find(1)
l.dayavailable_ids #=> Returns an array of the associated objects’ ids

But if you call dayavailable_id, it should be a column of table.

So l.dayavailable_ids will execute this DB query:

select id from dayavailables d
inner join locavailabilities l on l.dayavailable_id = d.id 
where l.location_id = 1 

but Locavailability.find(1).dayavailable_id will execute this DB query(not really, only for more readable):

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