在belongs_to关系中保存id时出现问题
我有 3 个对象:用户、旅行、积分。
一个用户有很多次旅行,一次旅行有很多积分,一个积分属于一个用户的一次旅行。
旅行还有一个布尔属性(:open),用于判断它是否处于诅咒状态。
问题是我无法将当前旅行的“travel_id”保存在我的积分表中。
这是代码:
class Point < ActiveRecord::Base
belongs_to :travel, :foreign_key=> "travel_id"
belongs_to :user, :foreign_key=> "user_id"
end
class Travel < ActiveRecord::Base
has_one :user, :foreign_key => "user_id"
has_many :ways
has_many :points
attr_accessible :description, :start_date, :last_date
validates_date :last_date, :on_or_after => :start_date
end
点控制器:
...
def create
@point = Point.new(params[:point])
@point.user_id = current_user.id
@travel = current_user.travels.find(:all, :conditions => {:open => true})
@point.travel_id = @travel.id
respond_to do |format|
if @point.save
format.html { redirect_to(@point, :notice => 'Point was successfully created.') }
format.xml { render :xml => @point, :status => :created, :location => @point }
else
format.html { render :action => "new" }
format.xml { render :xml => @point.errors, :status => :unprocessable_entity }
end
end
end
...
每次我尝试保存新点时,@point.travel_id = -614747648
I have 3 objects: users, travels, points.
A user has many travels, a travel has many points, a point belongs to one travel e one user.
A travel has also a boolean attribute (:open) which tells if is it in curse or not.
The problem is that I can't save "travel_id" of the current travel in my points table.
Here is the code:
class Point < ActiveRecord::Base
belongs_to :travel, :foreign_key=> "travel_id"
belongs_to :user, :foreign_key=> "user_id"
end
class Travel < ActiveRecord::Base
has_one :user, :foreign_key => "user_id"
has_many :ways
has_many :points
attr_accessible :description, :start_date, :last_date
validates_date :last_date, :on_or_after => :start_date
end
Points controller:
...
def create
@point = Point.new(params[:point])
@point.user_id = current_user.id
@travel = current_user.travels.find(:all, :conditions => {:open => true})
@point.travel_id = @travel.id
respond_to do |format|
if @point.save
format.html { redirect_to(@point, :notice => 'Point was successfully created.') }
format.xml { render :xml => @point, :status => :created, :location => @point }
else
format.html { render :action => "new" }
format.xml { render :xml => @point.errors, :status => :unprocessable_entity }
end
end
end
...
Every time I try to save a new point, @point.travel_id = -614747648
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里修复一下可以做一些事情。
首先,当键与关系名称 +
_id
相同时,您不需要指定:foreign_key
。其次,您不需要(通常也不应该)直接设置 foo_id 字段;更常见的做法是
@point.user = current_user
。第三,问题的直接原因是
@travel
已设置为find(:all, ...)
调用的结果 - 所以它是一个旅行对象的数组。您保存到@point.travel_id
中的内容将是@travel
数组的 Ruby 内部 ID,而不是单行的数据库 ID。A few things could do with being fixed up here.
Firstly, you don't need to specify
:foreign_key
when the key is just the same as the relation name +_id
.Second, you don't need to (and generally shouldn't) set the
foo_id
fields directly; it's more usual to do@point.user = current_user
.Thirdly, and the direct cause of your problem, is that
@travel
has been set to the result of afind(:all, ...)
call - so it's an Array ofTravel
objects. What you're saving into@point.travel_id
will be Ruby's internal id for the@travel
Array, rather than a database ID for a single row.