has_many :通过不授予对基类的_id 的访问权限
模型代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么
Location
应该有权访问dayavailable_id
?如果您给出 true,则
dayavailable_id
是locavailability
列:试试这个:
更新
但是如果你调用
dayavailable_id
,它应该是表的一列。所以
l.dayavailable_ids
将执行此数据库查询:但 Locavailability.find(1).dayavailable_id 将执行此数据库查询(不是真的,只是为了更具可读性):
Why
Location
should have access todayavailable_id
?The
dayavailable_id
is column oflocavailabilities
, if you given true:Try this:
UPDATED
But if you call
dayavailable_id
, it should be a column of table.So
l.dayavailable_ids
will execute this DB query:but Locavailability.find(1).dayavailable_id will execute this DB query(not really, only for more readable):