Hi! This problem kept me up all night. I can''t get my "has_and_belongs_to_many" relation to work. I want it between the models User and Artist. "User" is Login Generator-generated. Thus I have "has_and_belongs_to_many :artists" in the User class, and "has_and_belongs_to_many :users" in the Artist class. I have a table "artists_users" in the database (i use MySQL), which has an "artist_id" and "user_id" of type int(11). Now, when an Artist is created, I want to link this artist to the currently logged in user. I made a method in Artist: def add_user user @users << user end And then, in ArtistController, the create method, I have: @artist = Artist.new(@params[:artist]) raise unless @session[:user] @artist.add_user(@session[:user]) ..but here''s what I''m getting: undefined method `<<'' for nil:NilClass | /app/models/artist.rb:6:in `add_user'' app/controllers/artist_controller.rb:24:in `create'' ./script/server:48| So... Why is @users "nil" for this newly created Artist object? It works in the console; $ ./script/console Loading development environment. irb(main):001:0> Artist.new().users => [] I''ll be happy for any ideas! Simon Kågedal http://helgo.net/simon/
Simon Kågedal wrote:> Now, when an Artist is created, I want to link this artist to the > currently logged in user. I made a method in Artist: > > def add_user user > @users << user > end > > [...] > > So... Why is @users "nil" for this newly created Artist object? It works > in the console;Do users() instead of @users. I don''t think that the models use instance variables to store fields internally.
On 5/7/05, Florian Groß <florgro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Simon Kågedal wrote: > > > Now, when an Artist is created, I want to link this artist to the > > currently logged in user. I made a method in Artist: > > > > def add_user user > > @users << user > > end > > > > [...] > > > > So... Why is @users "nil" for this newly created Artist object? It works > > in the console; > > Do users() instead of @users. I don''t think that the models use instance > variables to store fields internally. >Florain''s right, it should be users instead of @users in the model. you can also remove add_user() altogether and use @artist.users << @session[:user] in your controller
Florian Groß wrote:> Simon Kågedal wrote: > >> Now, when an Artist is created, I want to link this artist to the >> currently logged in user. I made a method in Artist: >> >> def add_user user >> @users << user >> end >> >> [...] >> >> So... Why is @users "nil" for this newly created Artist object? It >> works in the console; > > > Do users() instead of @users. I don''t think that the models use > instance variables to store fields internally.Florian - thank you! That solved that problem. Now I remember, earlier yesterday night I had that part working by just doing "@artist.users << @session[:user]" from the controller. However, it brought me back to another problem: After having added the user, the artist will not save itself. @artist.save just returns false. Why? Am I supposed to also add the artist to the user, i.e. "@session[:user].artists << @artist" ? (that doesn''t help, though) Greetings, Simon Kågedal http://helgo.net/simon/
Simon Kågedal schrieb:> Florian Groß wrote: > >> Simon Kågedal wrote: >> >>> Now, when an Artist is created, I want to link this artist to the >>> currently logged in user. I made a method in Artist: >>> >>> def add_user user >>> @users << user >>> end >>> >>> [...] >>> >>> So... Why is @users "nil" for this newly created Artist object? It >>> works in the console; >> >> >> >> Do users() instead of @users. I don''t think that the models use >> instance variables to store fields internally. > > > > Florian - thank you! That solved that problem. Now I remember, earlier > yesterday night I had that part working by just doing "@artist.users << > @session[:user]" from the controller. > > However, it brought me back to another problem: After having added the > user, the artist will not save itself. @artist.save just returns false. > Why? >You can find out: user.errors.each_full{|err| logger.error err} My guess is, there is some validiation that gets in your way. Hope that helps. Henrik
Henrik Horneber schrieb:> Simon Kågedal schrieb: > >> Florian Groß wrote: >> >>> Simon Kågedal wrote: >>> >>>> Now, when an Artist is created, I want to link this artist to the >>>> currently logged in user. I made a method in Artist: >>>> >>>> def add_user user >>>> @users << user >>>> end >>>> >>>> [...] >>>> >>>> So... Why is @users "nil" for this newly created Artist object? It >>>> works in the console; >>> >>> >>> >>> >>> Do users() instead of @users. I don''t think that the models use >>> instance variables to store fields internally. >> >> >> >> >> Florian - thank you! That solved that problem. Now I remember, earlier >> yesterday night I had that part working by just doing "@artist.users >> << @session[:user]" from the controller. >> >> However, it brought me back to another problem: After having added the >> user, the artist will not save itself. @artist.save just returns >> false. Why? >> > > You can find out: > > user.errors.each_full{|err| logger.error err} >errr, make that @artist.errors.each_full{|err| logger.error err}
Personally I havent gotten this far in my usage but I remember this gotcha... perhaps this is it... has_and_belongs_to_many + validations I was trying to associate a user with an issue (for a simple ticket tracker), but every time I did: issue.users << user Things would *silently fail*. The problem turned out to be that the validations were again being checked for the user object (specifically the password_confirmation, which I didn’t have because the record had already been saved) . Adding ’:on => :create’ to that validation cleared up the problem. —Ben Bytheway <http://wiki.rubyonrails.com/rails/show/BenBytheway> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Henrik Horneber wrote:> Henrik Horneber schrieb: > >> Simon Kågedal schrieb: >> >>> Florian Groß wrote: >>> >>>> Simon Kågedal wrote: >>>> >>>>> Now, when an Artist is created, I want to link this artist to the >>>>> currently logged in user. I made a method in Artist: >>>>> >>>>> def add_user user >>>>> @users << user >>>>> end >>>>> >>>>> [...] >>>>> >>>>> So... Why is @users "nil" for this newly created Artist object? It >>>>> works in the console; >>>> >>>> >>>> >>>> >>>> >>>> Do users() instead of @users. I don''t think that the models use >>>> instance variables to store fields internally. >>> >>> >>> >>> >>> >>> Florian - thank you! That solved that problem. Now I remember, >>> earlier yesterday night I had that part working by just doing >>> "@artist.users << @session[:user]" from the controller. >>> >>> However, it brought me back to another problem: After having added >>> the user, the artist will not save itself. @artist.save just returns >>> false. Why? >>> >> >> You can find out: >> >> user.errors.each_full{|err| logger.error err} >> > > errr, make that > > @artist.errors.each_full{|err| logger.error err} > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/railsThank you! That helped a lot. There does indeed seem to be a validation problem ("Password confirmation can''t be empty", on the user). Reading up on that.. /simon