Ruby on Rails Saturday, January 29, 2011

Alexander Farley wrote in post #978434:
> So, here's my goal: I want an "intuitive" flow of "create user"->"create
> user's topics" -> "add blacklist for topic". Right now, my "edit user"
> view doesn't allow adding topics (topics must be added manually, with a
> known user_id).

So, you want to create a new user and automatically create a topic for
that user? Why? Are you just trying to add some seed data for the
user?

If so, you can do:

user = User.new(:email => 'bob@example.com', :hashed_password => '...')
user.create_topic(... your topic fields ...)

.. that's assuming you have the fields I expressed above.

When a model belongs to another, you automatically get the create_model
method..

But, if you aren't trying to automatically populate some seed data, why
are you trying to create a topic for a user?

> Am I going in the right direction? Is there a normal/default way of
> editing (adding) a table entry's "children"?

You should check out the methods for the model. Try the following in
your rails console:

User.methods.sort.each do |list|
p list
end

user = User.new
user.methods.sort.each do |list|
p list
end

The console is your best friend right now. I would check out the guides
on active record so you can gain a better understanding of how to work
with database records:

http://guides.rubyonrails.org/

Look over active record associations and active record callbacks.

You can handle the blacklist/raw/etc. that you are trying to work with
using before_create and after_create in the topic model. That way when
a topic is being created .. something is done.. and after it's created
something else is done..

With not too much to go on, this is all I can offer at the moment.

--
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment