sql INSERT of default data on new user creation ?

9 views
Skip to first unread message

Jeremy Savoy

unread,
Oct 20, 2011, 1:37:25 PM10/20/11
to Hobo Users
Each user in my application has a set of tasks that belong to them
only. There is a default set of 131 tasks that I need to be inserted
into the db when a new user is created, and those 131 tasks should be
owned by the new user.

I have an sql dump of the tasks, including an INSERT statement .. can
anyone here help me to understand how I might go about coding this sql
INSERT to occur right after a new user is created, using the new user
as the owner? I really have no idea where to start on this one.

Jeremy Savoy

unread,
Oct 20, 2011, 1:46:08 PM10/20/11
to Hobo Users
I did find the following examples for Rails, and the mass sql INSERT
is my chosen way, just not sure how or where to implement this in my
Hobo 1.3 app ...

http://www.coffeepowered.net/2009/01/23/mass-inserting-data-in-rails-without-killing-your-performance/

kevinpfromnm

unread,
Oct 20, 2011, 3:40:06 PM10/20/11
to hobo...@googlegroups.com
Probably best to put it in an after_create callback.  Excepting lifecycles, the rails methods are the hobo methods in the model side of things.  Only when you get to the view/controller layers that you might need/want to go to a hobo specific approach.

Jeremy Savoy

unread,
Oct 20, 2011, 5:09:02 PM10/20/11
to Hobo Users
What about in after_user_new ?

Is there an example that can be posted specifically to this situation?
I'm struggling to understand how the INSERT statement will take into
account the owner_id when it executes, as all records in this
application are owned by users. I can hardcode that value into the
INSERT/VALUE statement, but that doesn't really help because each new
user will have a different :user_id.

Can someone provide an example of how this might be accomplished?

kevinpfromnm

unread,
Oct 20, 2011, 5:46:38 PM10/20/11
to hobo...@googlegroups.com
That's why I suggested after_create, the user is already created and in the DB with a definite id.

after_create :add_default_models

def add_default_models
  # some code here using self.id (user model) wherever need the owning id.
end

Alternatively (and maybe better) is if you're using the lifecycle signup action you can put the model creation code there.

As far as passing the id in, most of the AR DB calls will accept an array where you'd normal think of a string that it properly SQL escapes to avoid injection attacks (not really a concern in this case since the id isn't something the end user can alter).

An example of use for a conditions:

:conditions => ["owner_id = ?",self.id]

The string is a sql fragment that has each ? replaced with the next value of the array down the line.  You need to supply a value for each ? even if they're all the same.
Reply all
Reply to author
Forward
0 new messages