[英]relationship belongs_to to belongs_to in ruby on rails
I have a relationship belongs_to to belongs_to between two model in rails. 我在Rails的两个模型之间有一个归属关系到归属关系。 I have the model user and the model Startup
我有模型用户和模型启动
The model User is a devise model (gem devise). 用户模型是一个设计模型(gem devise)。
this is my code. 这是我的代码。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :startup_name
# attr_accessible :title, :body
belongs_to :startup
end
And this is my model Startup 这是我的模型启动
class Startup < ActiveRecord::Base
attr_accessible :angelist, :capitalraise, :category, :country, :currentlocation, :description, :facebook, :linkedin, :name, :round, :startupchile, :twitter, :user_id
has_one :entrepreneus, dependent: :destroy
belongs_to :user
end
Also in my schema I have added "user_id" in Startup table. 同样在我的架构中,我在启动表中添加了“ user_id”。
this is the add_index added 这是添加的add_index
add_index "startups", ["user_id"], :name => "index_startups_on_user_id"
(Obviusly I did a migration) (显然我进行了迁移)
When I create a Startup in the first moment create the object without user_id, but with update_attributes add the user_id in the object. 当我在第一时间创建启动时,创建不带user_id的对象,但带update_attributes的对象将user_id添加到对象中。 (In the startup_controller).
(在startup_controller中)。 This is the code
这是代码
def create
@startup = Startup.new(params[:startup])
@startup.update_attributes(:user_id => current_user.id )
respond_to do |format|
if @startup.save
format.html { redirect_to @startup, notice: 'Startup was successfully created.' }
format.json { render json: @startup, status: :created, location: @startup }
else
format.html { render action: "new" }
format.json { render json: @startup.errors, status: :unprocessable_entity }
end
end
end
In the console I do the following (rails c --sandbox) 在控制台中,我执行以下操作(rails c --sandbox)
u = User.first (retrieve the user perfectly, the id is equal to 1)
u.startup (retrieve null)
But if you does this: 但是,如果您这样做:
s = Startup.find_by_user_id(1) // retrieve the data
Why I can't retrieve data associated to startup with u.startup ?. 为什么我无法通过u.startup检索与启动相关的数据?
Any idea ?. 任何想法 ?。 Thanks
谢谢
PDT: Sorry my english still is not very well. PDT:对不起,我的英语还不是很好。
I'm newer to rails so someone correct me if i'm wrong, but i don't think you can have 2 models that "belong_to" each other. 我是Rails的新手,所以如果我错了,请有人纠正我,但我认为您不能拥有2个彼此“属于”的模型。 1 has to have belong to and 1 has to have has_many or has_one (edit: as per comment, it should be
:has_one
. 1必须属于,1必须具有has_many或has_one(编辑:根据注释,应该为
:has_one
。
I think this may be what you're looking for: 我认为这可能是您要寻找的:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :startup_name
# attr_accessible :title, :body
has_one :startup # not has_many :startups
end
class Startup < ActiveRecord::Base
attr_accessible :angelist, :capitalraise, :category, :country, :currentlocation, :description, :facebook, :linkedin, :name, :round, :startupchile, :twitter, :user_id
has_one :entrepreneus, dependent: :destroy
belongs_to :user
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.