# Assign events on creation of a new user

**URL:** <https://www.ruby-forum.com/t/assign-events-on-creation-of-a-new-user/89770>\
**Category:** Rails\
**Created:** [March 22, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/assign-events-on-creation-of-a-new-user/89770 "2007-03-22T17:47:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinneyjd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b19c9b/32.png) [@kinneyjd](https://www.ruby-forum.com/u/kinneyjd)\
**Post date:** [March 22, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/assign-events-on-creation-of-a-new-user/89770/1 "2007-03-22T17:47:04Z")

</div>

Hello everyone,

I am developing a web app that is being used to track users and their  
associated events. Events could be like ‘getting married’, ‘having a  
baby’, ‘buying a car’, etc. and each event has a median age associated  
with it. For instance the getting married median age is 25.

I have the application working with checkboxes where you can click  
edit on a user and see all the possible events listed and the events  
that are associated with that user have a check in the checkbox next  
to the event.

See screen shot here: [http://www.jonkinney.com/rails/Picture3.png](http://www.jonkinney.com/rails/Picture3.png)

I am using a join model called occurrence.

the new task I am faced with is upon the creation of a new user I  
would like to auto associate all events with them based on the user’s  
age and the ages defined in the events. Eventually I would like to  
have it so that you can be within 5 years on either side of the median  
age defined for the event and it will auto associate upon creation of  
a new user.

Here is some of my code…

I was trying to get it to work with a callback in the user model  
called ‘before\_save :assign\_age\_items’

class User \< ActiveRecord::Base  
has\_many :occurrences, :dependent =\> :destroy #, :conditions =\>  
“enabled = 1”  
has\_many :events, :through =\> :occurrences

before\_save :assign\_age\_items

validates\_uniqueness\_of :username, :on =\> :create, :message =\> “must  
be unique”  
validates\_presence\_of :username, :on =\> :create, :message =\> “can’t  
be blank”  
validates\_presence\_of :password, :on =\> :create, :message =\> “can’t  
be blank”  
validates\_presence\_of :email, :on =\> :create, :message =\> “can’t be  
blank”  
validates\_presence\_of :fname, :on =\> :create, :message =\> “can’t be  
blank”  
validates\_presence\_of :lname, :on =\> :create, :message =\> “can’t be  
blank”  
validates\_presence\_of :gender, :on =\> :create, :message =\> “can’t be  
blank”

###### This doesn’t work

attr\_reader :user

def initialize(user)  
@user = user  
end

###### 

####### This does work…  
def event\_ids=(list)  
list ||= []  
occurrences.delete\_all  
events \<\< Event.find(list)  
end  
#######

####### This doesn’t work… assigning user’s default age specific  
items when first created…  
def assign\_age\_items  
year = Time.now.year  
birthyear = @user.dob.year  
age = year - birthyear

```
events << Events.find_by_age(age)

```

end

USER CONTROLLER…relevant code

def new  
@user = User.new  
end

def create  
@user = User.new(params[:user])  
if @user.save  
flash[:notice] = ‘User was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

I am trying to figure out how I can calculate the age of the user that  
is trying to sign up and then associate some events with them so that  
when they first view their home screen there are already some events  
that they MIGHT be interested in and then if they don’t want that info  
they can still manually edit it and remove the default events.  
Similarly if they want info that wasn’t initially suggested for them  
they will have the ability to search and add events manually (I have  
that working already I just can’t get the association upon creation).

Any insight into using the def initialize and how to pass parameters  
to it or whatever I need to do would be greatly appreciated. Also I’m  
confused what to do in my user\_controller with the def new and the def  
create. What is the difference? Should I be making those one method?

Thanks!  
-Jon

---

<div class="post-metadata">

**Author:** ![kinneyjd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b19c9b/32.png) [@kinneyjd](https://www.ruby-forum.com/u/kinneyjd)\
**Post date:** [March 23, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/assign-events-on-creation-of-a-new-user/89770/2 "2007-03-23T15:32:55Z")

</div>

Anyone have anything on this?

Thanks in advance…

---

<div class="post-metadata">

**Author:** ![kinneyjd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b19c9b/32.png) [@kinneyjd](https://www.ruby-forum.com/u/kinneyjd)\
**Post date:** [March 24, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/assign-events-on-creation-of-a-new-user/89770/3 "2007-03-24T18:47:17Z")

</div>

I understand I should be using a callback, in fact I am using one in  
the code that I provided for this post. I’m using the before\_save  
callback to well call back a function called ‘def assign\_age\_items’ My  
problem is that I don’t know how to get the into out of the user that  
just passed their info via the params. My function ‘def  
assign\_age\_items’ looks like this

def assign\_age\_items  
year = Time.now.year  
birthyear = @user.dob.year  
age = year - birthyear

```
events << Events.find_by_age(age)

```

end

I just want to be able to associate all events that are within the  
target user’s age to the user.

I’m probably going about this all wrong but ActiveRecord is still my  
biggest stumbling block with Rails. I’ve read literally all the  
Pragmatic books and I understand most of the stuff but anytime I want  
to deviate from a tutorial I find myself clueless as to how to get  
ActiveRecord to do something that I don’t have an exact code example  
of.

Thanks,  
-Jon

---

<div class="post-metadata">

**Author:** ![kinneyjd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b19c9b/32.png) [@kinneyjd](https://www.ruby-forum.com/u/kinneyjd)\
**Post date:** [March 23, 2007, 4:21pm UTC](https://www.ruby-forum.com/t/assign-events-on-creation-of-a-new-user/89770/4 "2007-03-23T16:21:02Z")

</div>

You might want to look into ActiveRecord’s callbacks or Observers. 🙂

RSL
