# Opinion on saving objects needed

**URL:** <https://www.ruby-forum.com/t/opinion-on-saving-objects-needed/143317>\
**Category:** Rails\
**Created:** [July 17, 2008, 9:56am UTC](https://www.ruby-forum.com/t/opinion-on-saving-objects-needed/143317 "2008-07-17T09:56:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 17, 2008, 9:56am UTC](https://www.ruby-forum.com/t/opinion-on-saving-objects-needed/143317/1 "2008-07-17T09:56:35Z")

</div>

Hello,

Well, right now I have the following models. I have three tables:  
users, events and attendances. Attendances is a join table that  
contains the event\_id, user\_id and rsvp (boolean representing if the  
user is going or not to the event). Just after creating a new event I  
save the event object on the events table and continue saving the user  
who created the event on the attendances table. Below I’ll show the  
code that does this. I just don’t feel it is the right way to do it,  
so your opinion is of great value on how it can be improved. Thanks!

class Event \< ActiveRecord::Base

```
has_many :attendances
has_many :users, :through => :attendances

```

end

class User \< ActiveRecord::Base

has\_many :attendances  
has\_many :events, :through =\> :attendances

end

class Attendance \< ActiveRecord::Base

belongs\_to :event  
belongs\_to :user

end

def create  
@event = Event.new(params[:event])  
@attendance = Attendance.new(:user\_id =\> current\_user.id)  
respond\_to do |format|  
if @event.save  
@attendance.event\_id = @event.id  
@attendance.save  
flash[:notice] = ‘Event was successfully created.’  
format.html { redirect\_to  
invite\_more\_friends\_neighborhood\_event\_path(@event.user.neighborhood,@event)  
}  
format.xml { render :xml =\> @event, :status  
=\> :created, :location =\> @event } if logged\_in? && admin?  
else  
# Added to show again all the event\_categories on the form  
flash[:error] = ‘Sorry, we found some errors.’  
@event\_categories = EventCategory.find(:all)  
@friends = current\_user.mutual\_friends  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @event.errors, :status  
=\> :unprocessable\_entity } if logged\_in? && admin?  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 17, 2008, 10:48am UTC](https://www.ruby-forum.com/t/opinion-on-saving-objects-needed/143317/2 "2008-07-17T10:48:08Z")

</div>

Basically that look like perfect working code.

You could do it a touch more compact like:

@event = Event.new(params[:event])  
if @event.save  
if @event.attendances.create(:user\_id =\> current\_user.id)  
…

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 17, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/opinion-on-saving-objects-needed/143317/3 "2008-07-17T17:04:53Z")

</div>

Thanks for your answer Thorsten. I got into something like this too:

@event.attendances \<\< current\_user.attendances.build

What do you think about it?

---

<div class="post-metadata">

**Author:** ![elioncho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ed8c4c/32.png) [@elioncho](https://www.ruby-forum.com/u/elioncho)\
**Post date:** [July 17, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/opinion-on-saving-objects-needed/143317/4 "2008-07-17T17:24:51Z")

</div>

Thanks for your answer Thorsten. I got into something like this too:

@event.attendances \<\< current\_user.attendances.build

What do you think about it?
