Rails association works when it doesnt use an instance variable -



Rails association works when it doesnt use an instance variable -

i'm getting strangest error i've seen in rails far. in view, can print out email associated painting if find record straight (e.g. painting.find(15). if seek utilize instance variable errors (e.g @painting).

views/paintings/show.html.erb

<%= painting.find(15).artist.user.email %> # works <%= @painting.artist.user.email %> # error: "undefined method 'user' nil:nilclass"

controllers/paintings_controller.rb

def show @painting = painting.find(15) end

models: "users", "artists", "paintings".

a user can artist. user has_one artist.

an artist has_many paintings.

i think should add together associations. how should like:

class user < activerecord::base has_one :artist # it's ok end class artist < activerecord::base belongs_to :user # add together has_many :paintings end class painting < activerecord::base belongs_to :artist end

for me both cases works associations.

ruby-on-rails ruby-on-rails-4 rails-activerecord

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -