# Accessing data that passes through the model

**URL:** <https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081>\
**Category:** Rails\
**Created:** [August 5, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081 "2008-08-05T20:34:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 5, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/1 "2008-08-05T20:34:34Z")

</div>

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 T.

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 5, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/2 "2008-08-05T21:30:49Z")

</div>

On 5 Aug 2008, at 19:34, Dustin T. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 5, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/3 "2008-08-05T22:02:45Z")

</div>

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 T.

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 6, 2008, 2:24am UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/4 "2008-08-06T02:24:59Z")

</div>

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. 😛 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

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 6, 2008, 9:40am UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/5 "2008-08-06T09:40:39Z")

</div>

On Aug 6, 1:24 am, Dustin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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.

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

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 5, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/6 "2008-08-05T22:40:50Z")

</div>

On 5 Aug 2008, at 21:01, Dustin T. wrote:

> member has enough credits to pay for his or her story post. The only

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

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 6, 2008, 4:49pm UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/7 "2008-08-06T16:49:45Z")

</div>

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 T.

---

<div class="post-metadata">

**Author:** ![tinney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a88e4f/32.png) [@tinney](https://www.ruby-forum.com/u/tinney)\
**Post date:** [August 6, 2008, 11:09pm UTC](https://www.ruby-forum.com/t/accessing-data-that-passes-through-the-model/145081/8 "2008-08-06T23:09:56Z")

</div>

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
