Re: [Rails] Re: Do associations work both ways?

2010-12-13 Thread Rob Biedenharn
On Dec 12, 2010, at 4:59 PM, Rodrigo Mendonça wrote: Great!! when user_id is null (and can be null in some cases) you can do this %post.each do |p|% %=p.user.name unless p.user.nil?% %end other way is %=p.user.name unless p.user.empty?% Or, you can push that into the model and avoid

[Rails] Re: Do associations work both ways?

2010-12-13 Thread Aston J.
Thanks for the replies Rob and Marnen - they are both very helpful to me as a nube :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Talk group. To post to this group, send email to

Re: [Rails] Re: Do associations work both ways?

2010-12-13 Thread Michael Pavling
On 13 December 2010 16:30, Rob Biedenharn r...@agileconsultingllc.com wrote: Or, you can push that into the model and avoid the extra logic in the view: class Post   def user_name     self.user ? self.user.name : (none)   end end Instead of creating a new method to delegate from the

[Rails] Re: Do associations work both ways?

2010-12-12 Thread Frederick Cheung
On Dec 12, 6:38 pm, Ast Jay li...@ruby-forum.com wrote: In that loop, can I do: td% post.user %/td ...to give me back the user that made that post? In the rails console I can do user.posts (that gives me all the posts a user has made) but I can't do post.user (to give me the user). As

[Rails] Re: Do associations work both ways?

2010-12-12 Thread Ast Jay
Hi Fred, thanks for the reply. In the rails console with 'post.user' I get: NoMethodError: undefined method `user' for #Post:0x0101455590 And in my view 'post.user.nameI get: undefined method `name' for nil:NilClass If I leave off '.name' I get what looks like an object:

Re: [Rails] Re: Do associations work both ways?

2010-12-12 Thread Rodrigo Mendonça
It's not normal try reboot your server check in DB if user_id is null try Post.first.user.name 2010/12/12 Ast Jay li...@ruby-forum.com Hi Fred, thanks for the reply. In the rails console with 'post.user' I get: NoMethodError: undefined method `user' for #Post:0x0101455590 And in my

[Rails] Re: Do associations work both ways?

2010-12-12 Thread Ast Jay
Hi Rodrigo, thanks for the reply. I've tried that - but still doesn't work :( I pushed the app to git: https://github.com/Brook/uf/blob/master/app/views/topics/index.html.erb And you can see where I'm trying to call it on line 18 Maybe you can spot where I've gone wrong? :/ -- Posted via

Re: [Rails] Re: Do associations work both ways?

2010-12-12 Thread Rodrigo Mendonça
Search in app/model for the file User.rb (in singular) This file exists?? Paste the code of this file here. 2010/12/12 Ast Jay li...@ruby-forum.com Hi Rodrigo, thanks for the reply. I've tried that - but still doesn't work :( I pushed the app to git:

[Rails] Re: Do associations work both ways?

2010-12-12 Thread Ast Jay
Thank you both for your help - I got it fixed with a little help from Isaackearse on the Rails IRC channel. The reason it was playing up was because I had some posts in the db that weren't associated to any users - once they were deleted everything works as expected :) Thanks again to

Re: [Rails] Re: Do associations work both ways?

2010-12-12 Thread Colin Law
2010/12/12 Rodrigo Mendonça den...@gmail.com: Search in app/model for the file User.rb (in singular) This file exists?? For posterity that should be user.rb not User.rb of course Colin -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Talk group. To

Re: [Rails] Re: Do associations work both ways?

2010-12-12 Thread Rodrigo Mendonça
Great!! when user_id is null (and can be null in some cases) you can do this %post.each do |p|% %=p.user.name unless p.user.nil?% %end other way is %=p.user.name unless p.user.empty?% 2010/12/12 Colin Law clan...@googlemail.com 2010/12/12 Rodrigo Mendonça den...@gmail.com: Search in

[Rails] Re: Do associations work both ways?

2010-12-12 Thread Ast Jay
Thanks Rodrigo. Is that only for db fields that are part of the 'join'/association? Because I had some other fields that were empty too (but they belonged to the post model) but they didn't cause any probs. -- Posted via http://www.ruby-forum.com/. -- You received this message because you

Re: [Rails] Re: Do associations work both ways?

2010-12-12 Thread Rodrigo Mendonça
No, no, if field is null or empty you can use this. It's not specifc for join or association You are learning the ROR, and is better for you use .empty? It's will cause less errors for you Sorry for my english.. i'm still learning =D 2010/12/12 Ast Jay li...@ruby-forum.com Thanks Rodrigo.

[Rails] Re: Do associations work both ways?

2010-12-12 Thread Ast Jay
Thanks again Rodrigo, and no worries about your English... it is very good! If you're on twitter, please feel free to add me :) my username is: AstonJ -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups Ruby on Rails: Talk