Accessing data that passes through the model

Greetings!

I'm working on building a custom validator within my Story model. As I understand it, before anything can be put into the database, that data has to go through the model and the various validators. Is there a way to access this data?

The data should contain a foreign key that links to the member who is posting the story. I want to be able to compare the data from this member against the story data.

A simple point in the right direction would be tremendously helpful. Thank you for your time!

~Dustin Tigner

Greetings!

I'm working on building a custom validator within my Story model. As I understand it, before anything can be put into the database, that data has to go through the model and the various validators. Is there a way to access this data?

That's not quite how it works: all the attributes and so on are
changed and so on, so foo.some_attribute does return the new data
(even though it hasn't hit the database yet) (or have I missed the
point?)

Fred

Hey Frederick,

That's not quite how it works: all the attributes and so on are changed and so on, so foo.some_attribute does return the new data (even though it hasn't hit the database yet) (or have I missed the point?)

What I'm trying to do (which may or may not be correct), is create a validator within my model file: story.rb, that checks to see if the member has enough credits to pay for his or her story post. The only way I can do this is if I knew how much money the member has in his or her account. However, I don't know how to pull that data from within the story model (if it's possible / the Rails way to do so).

(This is per my current knowledge): When a member submits a form, that form should go to the create action from within the controller. When you try to save the data to the database, it's checked against the validators within the model and either returns true or false. Thus, the story model should receive and check the data from the form. If that's true, is there a way to see what data the model is handling?

Perhaps I should be doing this check from within the controller? Such as:

def create   @story = Story.new(params[:story])   @story.member_id = current_user.id

  unless current_user.credits >= (@story.credits * @story.critiques_wanted)     errors.add_to_base("You do not have enough credits to post this story.")   end

  if @story.save     flash[:notice] = 'Story was successfully created.'     redirect_to manage_stories_path   else     render :action => "new"   end end

I haven't tried this code. I figure it will work (probably with a few adjustments), though am not sure it's the Rails way. I'm under the impression that all validation should be done within the model. Is this correct thinking, or is what I have above okay?

Thanks for your help Frederick. You always seem to be close whenever I'm plagued with my many questions.

~Dustin Tigner

Hey Frederick,

That's not quite how it works: all the attributes and so on are changed and so on, so foo.some_attribute does return the new data (even though it hasn't hit the database yet) (or have I missed the point?)

What I'm trying to do (which may or may not be correct), is create a validator within my model file: story.rb, that checks to see if the member has enough credits to pay for his or her story post. The only way I can do this is if I knew how much money the member has in his or her account. However, I don't know how to pull that data from within the story model (if it's possible / the Rails way to do so).

(This is per my current knowledge): When a member submits a form, that form should go to the create action from within the controller. When you try to save the data to the database, it's checked against the validators within the model and either returns true or false. Thus, the story model should receive and check the data from the form. If that's true, is there a way to see what data the model is handling?

You're imagining a difficultly. If you want to know how many credits
the user has, then do it in exactly he same way as any other time, if
you want to check some value of the model then again just do it, eg def validate    if member.credits >= credits * critiques_wanted      ...    end end

That said I don't think a validation is the right tool in this case.
You wouldn't want this check to run again if the story was being saved
because it had been updated.

Fred

Greetings Fred,

You're imagining a difficultly. If you want to know how many credits the user has, then do it in exactly he same way as any other time, if you want to check some value of the model then again just do it, eg def validate    if member.credits >= credits * critiques_wanted      ...    end end

I believe you are right - I'm creating my own frustration. :stuck_out_tongue: For the life of me, I couldn't pull the members information into the Story model. Even your little snippet of code above didn't work at first. Making sure I wasn't crazy I started doing a whole lot of little experiments to see what actually was happening. I was even reading my terminal that displayed everything my server was doing. Then, just like Rails magic and mystery, everything just worked - flawlessly. Why...? I spent the following 30 minutes trying to figure out what I had changed. Perhaps instead of member.credits, I wrote memers.credits - members being plural instead of singular. I hope that wasn't the problem. >< If so, too much drama for one measly little 's'.

That said I don't think a validation is the right tool in this case. You wouldn't want this check to run again if the story was being saved because it had been updated.

I still believe that validation is the correct way to do it. For example, if the member changes the credits and critiques_wanted in an update, I would still like to insure they have enough credits to do so. If I'm missing something, please let me know.

Thanks for all your help!

Fred

~Dusitn Tigner

There may well be some updates that require checking of credits, but I wouldn't think all would. Eg if the user spots a typo in what they've written and want to fix it, you wouldn't want them to be prevented from doing that because of a failing validation. I'm just guessing of course - you're the one with full knowledge of how the application is used and the business model.

Fred

Greetings,

There may well be some updates that require checking of credits, but I wouldn't think all would. Eg if the user spots a typo in what they've written and want to fix it, you wouldn't want them to be prevented from doing that because of a failing validation. I'm just guessing of course - you're the one with full knowledge of how the application is used and the business model.

That could very well be true. To be honest, I'm not entirely sure how I want the application to work in that regard. I'll keep your words in mind as I work my way through trial and error. Thanks for your constant support!

Fred

~Dustin Tigner

Along the same lines as Fred’s comment, I would recommend accepting the story but not offering it up for review until the user pays up. I think you would have a better chance of getting them to come back and just pay than to do the entire process over again.

Sunny