Validation

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”>
#<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/
feed/rss_v2_0_msgs.xml |
| 2 | 2 | http://www.slashdot.org/
index.rss |
| 3 | 2 | http://groups.google.ca/group/rubyonrails-talk/
feed/rss_v2_0_msgs.xml |
| 4 | 1 | 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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
[email protected]