在这个例子中,我创建了一个user没有profile,然后在profile为该用户创建一个.我尝试使用带有has_one关联的构建,但是爆炸了.我看到这个工作的唯一方法是使用has_many.本user应该只有最多只能有一个profile.
我一直在尝试这个.我有:
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
但当我这样做时:
user.build_profile
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'profiles.user_id' in 'where clause': SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4) LIMIT 1
Run Code Online (Sandbox Code Playgroud)
在rails中有一种方法可以有0或1个关联吗?
我想了解has_oneRoR中的关系.
假设我有两个模型 - Person并且Cell:
class Person < ActiveRecord::Base
has_one :cell
end
class Cell < ActiveRecord::Base
belongs_to :person
end
Run Code Online (Sandbox Code Playgroud)
我可以只使用has_one :person,而不是belongs_to :person在Cell模型?
不一样吗?
假设我有一个grails域类,看起来像
class Person {
Address address
}
Run Code Online (Sandbox Code Playgroud)
我也可以声明它
class Person {
static hasOne = [address:Address]
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是将外键移动到Address表而不是person表.
这种方式与另一种方式有什么实际的好处(或缺点)?据我所知,他们都会使用外键,这只是外键所在的问题.
我正在尝试在"问题"模型中更新嵌套的question_output属性.一个问题has_one question_output.如果数据库中没有现有的question_output,一切正常.但是如果记录已经有一个question_output,我在尝试更新时会得到以下结果:
无法删除现有的关联question_output.在将外键设置为nil之后,记录无法保存.
我本以为allow_destroy可以解决这个问题,但是唉 - 没有快乐.不可否认,我之前没有使用过has_one.但如果有人对如何解决这个问题有任何想法,我会很感激.相关代码如下:
表格:
= form_for [@question.project, @question], :as => :question, :url => admin_project_question_path(@question.project, @question) do |f|
= render '/shared/form_errors', :model => @question
= f.fields_for :question_output_attributes do |qo|
.field
= qo.label :question_type
= qo.select :question_type, QuestionOutput::QUESTION_TYPES
.field
= qo.label :client_format
= qo.select :client_format, QuestionOutput::CLIENT_FORMATS
.field
= qo.label :required
= qo.check_box :required
.field
= qo.label :min_input, 'Length'
= qo.text_field :min_length
= qo.text_field :max_length
= f.submit 'Save Question Formatting'
Run Code Online (Sandbox Code Playgroud)
问题模型:
class Question < ActiveRecord::Base
has_one :question_output
accepts_nested_attributes_for …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我的用户可以拥有一组首选项.两者都存储为ActiveRecord模型,如下所示:
class User < AR::Base
has_one :preference_set
end
class PreferenceSet < AR::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
我现在可以访问用户的首选项:
@u = User.first
@u.preference_set => #<PreferenceSet...>
@u.preference_set.play_sounds => true
Run Code Online (Sandbox Code Playgroud)
但是,这是否偏好设置尚未创建,因为@ u.preference_set将返回nil我将调用失败,play_sounds上nil.
我想归档的是User.preference_set总是返回一个PreferenceSet实例.我试过像这样定义它:
class User < ..
has_one :preference_set
def preference_set
preference_set || build_preference_set
end
end
Run Code Online (Sandbox Code Playgroud)
这导致a 'Stack level too deep',因为它以递归方式调用自身.
我的问题是:
如何确保@user.preference_set返回相应的preference_set-record,如果不存在则返回构建新的记录?
我知道我可以重命名我的关联(例如preference_set_real)并以这种方式避免递归调用,但为了我的应用程序的简单性,我想保留命名.
谢谢!
我有两节课:
class User < ActiveRecord::Base
:has_one :foo
end
class Foo < ActiveRecord::Base
:belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
Foo是可选的.
我创建了以下路由:
resources :users do
resources :foo
end
Run Code Online (Sandbox Code Playgroud)
这导致以下路线:
GET /users/:user_id/foo(.:format) {:controller=>"foos", :action=>"index"}
user_foos POST /users/:user_id/foo(.:format) {:controller=>"foos", :action=>"create"}
new_user_foo GET /users/:user_id/foo/new(.:format) {:controller=>"foos", :action=>"new"}
GET /users/:user_id/foo/:id(.:format) {:controller=>"foos", :action=>"show"}
PUT /users/:user_id/foo/:id(.:format) {:controller=>"foos", :action=>"update"}
user_foo DELETE /users/:user_id/foo/:id(.:format) {:controller=>"foos", :action=>"destroy"}
edit_user_foo GET /users/:user_id/foo/:id/edit(.:format) {:controller=>"foos", :action=>"edit"}
Run Code Online (Sandbox Code Playgroud)
问题:
谢谢你的时间.
我有一个users表和user_profiles表的应用程序.用户has_one用户配置文件和用户配置文件belongs_to.
我想确保关联方案始终为true,因此我对两个外键的存在进行了验证.问题是我遇到了"鸡蛋和鸡蛋"的情况.当我创建用户时,它不起作用,因为用户配置文件尚不存在,当我创建用户配置文件时,它也不起作用,因为用户尚不存在.所以我需要在创建用户的过程中创建用户配置文件.更复杂的是,当我创建客户端时,我还在after_create回调中创建了一个用户.足够的谈话(或阅读/写作),这里有一些代码:
class User < ActiveRecord::Base
has_one :user_profile
validates :user_profile_id, presence: true
end
class UserProfile < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
end
class Client < ActiveRecord::Base
after_create :create_client_user
private
def create_client_user
User.create!(
email: "admin@example.com",
password: "admin",
password_confirmation: "admin",
client_id: self.id
# I need to create a user profile dynamically here
)
end
end
Run Code Online (Sandbox Code Playgroud)
有可能做我想做的事吗?
我尝试了@cdesrosiers建议的解决方案,但我不能让我的规格通过.我主要有三个错误.首先让我告诉你更新的模型:
class User < ActiveRecord::Base
has_one :user_profile, inverse_of: :user
before_create { build_user_profile } …Run Code Online (Sandbox Code Playgroud) RailsGuides说:
http://guides.rubyonrails.org/association_basics.html 一个has_many"关联表示模型的每个实例都有零个或多个另一个模型的实例."
"has_one关联还与另一个模型建立一对一的连接,但语义(和后果)有些不同.这种关联表明模型的每个实例都包含或拥有另一个模型的一个实例."
这是否意味着如果我想建立一个关联,模型的每个实例都有零或另一个模型的一个实例,最好的方法是使用has_many而不是has_one?如果我使用has_one会遇到什么问题?
谢谢.
在登录模型中,我与图片表有实现关系
function picture () {
return $this->hasOne('App\Picture');
}
Run Code Online (Sandbox Code Playgroud)
现在我想要 Picture.picture_status = 1 和 User.user_status = 1 的数据
Login::with('picture')->where('picture_status', 1)->where('user_status',1);
Run Code Online (Sandbox Code Playgroud)
但是如果条件不适用于图片表,我如何在两个表上实施和条件
我希望Status在一些用户定义的设置之后有一个相对静态的模型(不同的用户可能在状态上有不同的值).
状态可以应用于不同的模型,例如Contact和Event.
所以返回的状态contact.status将不同于event.status
我想设计应用程序,以便状态表具有不同的类型(contacts和events).
什么是正确的策略和格式?
我正在考虑:has_one Status在Contact模型中声明,并:status_id在:contacts表中存储.同上Event.
:statuses table将具有状态值,类型和日期.
这有意义吗?你能建议一个更好的方法吗?
has-one ×10
associations ×3
belongs-to ×2
activerecord ×1
foreign-keys ×1
grails ×1
grails-orm ×1
has-many ×1
laravel ×1
routing ×1
ruby ×1