# Validation

**URL:** <https://www.ruby-forum.com/t/validation/180649>\
**Category:** Rails\
**Created:** [December 25, 2009, 3:18pm UTC](https://www.ruby-forum.com/t/validation/180649 "2009-12-25T15:18:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 25, 2009, 3:18pm UTC](https://www.ruby-forum.com/t/validation/180649/1 "2009-12-25T15:18:01Z")

</div>

My understanding is that for a rails (or active record) app, the data  
validation occurs in the model? While my first inclination is to  
prevent  
the rss feed from showing up twice in the “subscriptions” model, this is  
the wrong approach, in that two users might subscribe to the same feed.  
Actually, that would be a many-to-many relationship, so a join table is  
required. A reasonable approach? And then, ensure that the list of  
subscriptions is unique?

thufir@ARRAKIS:~/projects/rss2mysql$  
thufir@ARRAKIS:~/projects/rss2mysql$ ruby query.rb  
“--------begin subscriber-----------”  
#\<Subscriber id: 1, subscriber: “alpha”\>  
#\<Subscription id: 1, subscriber\_id: 1, subscription: “http://  
groups.google.ca/group/ruby-talk-google/feed…”\>  
#\<Subscriber id: 1, subscriber: “alpha”\>  
#\<Subscription id: 4, subscriber\_id: 1, subscription: “http://  
groups.google.ca/group/rubyonrails-talk/feed…”\>  
“----------end subscriber---------------”  
“--------begin subscriber-----------”  
#\<Subscriber id: 2, subscriber: “beta”\>  
#\<Subscription id: 2, subscriber\_id: 2, subscription: “http://  
[Slashdot](http://www.slashdot.org/index.rss)”\>  
#\<Subscriber id: 2, subscriber: “beta”\>  
#\<Subscription id: 3, subscriber\_id: 2, subscription: “http://  
groups.google.ca/group/rubyonrails-talk/feed…”\>  
“----------end subscriber---------------”  
thufir@ARRAKIS:~/projects/rss2mysql$  
thufir@ARRAKIS:~/projects/rss2mysql$ mysql -u ruby -p  
Enter password:  
Welcome to the MySQL monitor. Commands end with ; or \g.  
Your MySQL connection id is 87  
Server version: 5.1.37-1ubuntu5 (Ubuntu)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input  
statement.

mysql\> use rss2mysql;  
Reading table information for completion of table and column names  
You can turn off this feature to get a quicker startup with -A

Database changed  
mysql\> select \* from subscriptions;  
±—±--------------  
±----------------------------------------------------------------------+  
| id | subscriber\_id |  
subscription |  
±—±--------------  
±----------------------------------------------------------------------+  
| 1 | 1 | [http://groups.google.ca/group/ruby-talk-google/](http://groups.google.ca/group/ruby-talk-google/)  
feed/rss\_v2\_0\_msgs.xml |  
| 2 | 2 | [http://www.slashdot.org/](http://www.slashdot.org/)  
index.rss |  
| 3 | 2 | [http://groups.google.ca/group/rubyonrails-talk/](http://groups.google.ca/group/rubyonrails-talk/)  
feed/rss\_v2\_0\_msgs.xml |  
| 4 | 1 | [http://groups.google.ca/group/rubyonrails-talk/](http://groups.google.ca/group/rubyonrails-talk/)  
feed/rss\_v2\_0\_msgs.xml |  
±—±--------------  
±----------------------------------------------------------------------+  
4 rows in set (0.00 sec)

mysql\> quit  
Bye  
thufir@ARRAKIS:~/projects/rss2mysql$  
thufir@ARRAKIS:~/projects/rss2mysql$ nl query.rb  
1 require ‘rubygems’  
2 require ‘active\_record’  
3 require ‘yaml’  
4 require ‘pp’  
5 require ‘subscription’  
6 require ‘subscriber’

```
 7 db = YAML::load(File.open('database.yml'))

 8 ActiveRecord::Base.establish_connection(
 9 :adapter => db["development"]["adapter"],
10 :host => db["development"]["host"],
11 :username => db["development"]["username"],
12 :password => db["development"]["password"],
13 :database => db["development"]["database"])

14 subscribers = Subscriber.find(:all)

15 subscribers.each do |subscriber|
16 pp "--------begin subscriber-----------"
17 subscriptions = subscriber.subscriptions.find(:all)
18 subscriptions.each do |subscription|
19 pp subscriber
20 pp subscription
21 end
22 pp "----------end subscriber---------------"
23 end

```

thufir@ARRAKIS:~/projects/rss2mysql$

thanks,

Thufir

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [December 26, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/validation/180649/2 "2009-12-26T18:00:11Z")

</div>

Thufir wrote:

> My understanding is that for a rails (or active record) app, the data  
> validation occurs in the model?

Data validation occurs (or should occur) in the database in most cases  
(BTW, this works better in PostgreSQL than MySQL). The ActiveRecord  
validations are only a backup.

> While my first inclination is to  
> prevent  
> the rss feed from showing up twice in the “subscriptions” model, this is  
> the wrong approach, in that two users might subscribe to the same feed.  
> Actually, that would be a many-to-many relationship, so a join table is  
> required. A reasonable approach? And then, ensure that the list of  
> subscriptions is unique?

I don’t know. What’s your business logic here? You neglected to  
explain it.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
