Jason Yost
Jason Yost

Reputation: 4937

Pass object to before_filter to perform additional logic before create

I need perform some extra logic on an object before it is saved to the database. I would assume that using a before_filter would be the correct way to accomplish this, but I'm not sure how to pass the object to be saved into my before_filter method.

Upvotes: 0

Views: 425

Answers (2)

iNulty
iNulty

Reputation: 923

This is my first ever post here so go easy on me!

Theres a before_save method, in Rails 3 at least, that can be called in the model. in Posts model

before_save :add_url_and_ID


def add_url_and_ID
#extra logic
self[:url] = whatever.com
self[:member_id] = member.id
end

I'm probably way off but its my first go!

Upvotes: 1

RSG
RSG

Reputation: 7123

That all sounds like model code to me. To get the most out of Rails (or any MVC framework) follow the 'Fat Models, Skinny Controllers' rule of thumb. It can be taken too far, but I think in this case it's pretty clear. If there are errors with the helper functions you mention, shouldn't the save action fail with appropriate error messages?

There's lots of good posts on SO on this subject. Here's one

If this doesn't give you enough to work with I'd suggest posting some code.

Upvotes: 0

Related Questions