Ruby on rails 基于一对一关系的Rails分配
好的,我正试图通过一对一的关系将概要文件对象连接到会话对象。我的理解是,如果我正确地建立了关系,那么以下内容是等效的(如果我错了,请纠正我) 但是,每当我尝试执行以下操作时:Ruby on rails 基于一对一关系的Rails分配,ruby-on-rails,Ruby On Rails,好的,我正试图通过一对一的关系将概要文件对象连接到会话对象。我的理解是,如果我正确地建立了关系,那么以下内容是等效的(如果我错了,请纠正我) 但是,每当我尝试执行以下操作时: @my_session << @my_profile @my_session在ActiveRecord模型上声明一个具有一个关联时,它获得以下方法: association(force_reload = false) association=(associate) build_association(attr
@my_session << @my_profile
@my_session在ActiveRecord模型上声明一个具有一个关联时,它获得以下方法:
association(force_reload = false)
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})
这不包括电铲操作员好的,所以你的意思是,如果我做1-1关系,我就不能使用Yes。在红宝石中,真是太棒了。谢谢你的澄清!没问题。我个人会这样写@my_session.profile=@my_profile
。然后用铲子铲一个假想的有很多像这样的@my_profile.favorites
class Session < ActiveRecord::Base
# I tried this without foreign_key also, it works the same
belongs_to :profile, :foreign_key => 'profile_id'
@my_session.profile = @my_profile
@my_session.profile_id = @my_profile.id
@my_session << @my_profile
NoMethodError: undefined method `<<' for #<Session:0x00000004a26198>
from /.../rubies/ruby-2.2.2/lib/ruby/gems/2.2.0/gems/activemodel-4.2.3/lib/active_model/attribute_methods.rb:433:in `method_missing'
association(force_reload = false)
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})
collection(force_reload = false)
collection<<(object, ...)
collection.delete(object, ...)
collection.destroy(object, ...)
collection=(objects)
collection_singular_ids
collection_singular_ids=(ids)
collection.clear
collection.empty?
collection.size
collection.find(...)
collection.where(...)
collection.exists?(...)
collection.build(attributes = {}, ...)
collection.create(attributes = {})
collection.create!(attributes = {})