@blog = Blog.create(name:'My First Blog')
@post = @blog.posts.create(name: 'My first post in My First Blog')
# post is now created and saved
@blog = Blog.find_by_name: 'My First Blog'
@post = @blog.posts.create(name: 'My second post in My First Blog')
# post is now created and saved
@blog = Blog.first
@post = @blog.posts.build(name: 'My third post in my My First Blog')
# post is being built but has not yet been saved
@post.save!
# post is now saved
Probably the easiest thing you can do is understand the types of methods
that are available to each object through their association.
Example (from rails console):
@blog = Blog.first
@blog.methods.sort.each do |method|
p method
end
Or, single line it for posts (just writing it different ways):
@blog.posts.methods.sort.map{ |method| p method }
--
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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/446fb73e200a123328a326ea1440228f%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment