I'm rolling with a legacy database unfortunately and am trying to build my rails3 app around it.
Thanks to this previous post, I've figured out where I'm going but still think I'm approaching incorrectly.
My basic problem is that main my main data is stored in a table with multiple rows, each with a different attribute value:
+-----+----------+----------------+----+---------------+------------+
| id | username | attribute_name | op | value | raduser_id |
+-----+----------+----------------+----+---------------+------------+
| 173 | jenny | User-Password | := | March 25 2011 | 33 |
| 172 | jenny | User-Password | := | 1234 | 33 |
+-----+----------+----------------+----+---------------+------------+
2 rows in set (0.00 sec)
I was using a nested form to enter this information but it's not really doing what I need. I can add the nested attributes and set a field thanks to the previous question now.
The issue I have is that I need some more control over my user's inputs. For instance, I need to restrict them to three distinct attributes:
User-Password, Expiration, Simultaneous-Use
I also need to validate the fields. I can't do so with the nested form.
My plan was to get the user to enter these in the parent model and propagate down but I do not have a clue how to do this and save out to separate rows, like I do with my nested atributes.
Can anyone shed any light on this?
--UPDATE--
raduser.rb
class Raduser < ActiveRecord::Base
has_many :radcheck, :dependent => :destroy
accepts_nested_attributes_for :radcheck, :reject_if => lambda { |a| a[:value].blank? }, :allow_destroy => true
end
radcheck.rb
class Radcheck < ActiveRecord::Base
set_table_name 'radcheck'
attr_accessible :attribute_name, :username, :value, :op, :groupname
belongs_to :raduser
has_many :radusergroup, :dependent => :destroy, :primary_key => :username, :foreign_key => :groupname
has_many :radgroupcheck, :through => :radusergroup
before_save :sync_usernames
private
def sync_usernames
self.username = self.raduser.username
end
end