#的未定义方法`id'

时间:2015-02-23 07:06:39

标签: ruby-on-rails-4 associations rails-activerecord

抱歉愚蠢的问题

这时我正在筹集@property我正在排队但是当我正在筹集@property.id时,它会显示undefined method id给我这个结论

owner.rb

中的

has_many :properties
property.rb

中的

belongs_to :owner

in owners_controller.rb

def new    
user = User.friendly.find(current_user.slug)
@owner = user.owner
authorize @owner
@property = Property.where(owner_id: @owner)
#raise @property.id.inspect
@renter = User.friendly.find(params[:renter_id]).renter  
@message = Message.new(renter_id: @renter.id, owner_id: @owner.id,property_id: @property.id)
end

提前致谢

1 个答案:

答案 0 :(得分:3)

正如我所说,where会返回一个Active Record关系array。试试下面的

raise @property.first.id

更改此

@property = Property.where(owner_id: @owner)

@property = Property.find_by(owner_id: @owner)