Ruby on Rails Sunday, October 2, 2011

On Sep 23, 4:47 pm, DanS <thehappyd...@gmail.com> wrote:
> Hello, I am new to rails and I need a little help getting started with
> a simple association. Here is what I am trying to do.
>
> I have a model, message, which has text and a user (do not need to
> worry about associating users to messages right now). These messages
> come in and I would like to create a response for each, which itself
> is a message. Then I would like to create a transaction paring these
> responses and requests.
>
> @request = Message.create(:text => "Some text")
> @response = Message.create(:text => "Some text back")
> @some_transaction = Transaction.create(:request =>
> @request.id, :response => @response.id)
>
> From there I would like to be able to sort through each transaction:
> @some_transaction.request.text
>
> How are these associations formed? When generating the models do I
> need to have columns for request/response or request_id/response_id?

Here's a good place to start reading:

http://guides.rubyonrails.org/association_basics.html

Some other notes:

- 'Transaction' is not going to work as a model name; ActiveRecord
uses that name internally, and redefining it may cause extremely
peculiar malfunctions.

- if messages are paired strictly one-to-one with replies, it may make
more sense to omit the intermediate model entirely and just define a
has_one/belongs_to pair.

--Matt Jones

--
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