0

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
2
  • I am not sure about your data model..can u give the fields in radusers table also and how the entities raduser and radcheck are related? Commented Nov 22, 2011 at 4:33
  • @rubyprince Sorry, don't know why I didn't see your comment before. Will do so now. Commented Nov 23, 2011 at 11:58

1 Answer 1

0

Did you try placing the validations in the radcheck.rb model? Try this code:

radcheck.rb

class Radcheck < ActiveRecord::Base
  set_table_name 'radcheck'
  attr_accessible :attribute_name, :username, :value, :op, :groupname
  belongs_to :raduser

  validates :attribute_name, :inclusion => { :in => %w(User-Password Expiration Simultaneous-Use) }

  before_save :sync_usernames

  private

  def sync_usernames
    self.username = self.raduser.username
  end
end

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

radusers_controller.rb

def new
  @raduser = Raduser.new
  @raduser.radcheck.build
end

def create
  @raduser = Raduser.new(params[:raduser])
  if @raduser.save
    redirect_to(@raduser, :notice => 'Raduser was successfully created.')
  else
    render :action => "new"
  end
end

and finally the form

<% if @raduser.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@raduser.errors.count, "error") %> prohibited this raduser from being saved:</h2>

    <ul>
      <% @raduser.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

<%= form_for @raduser do |f| %>  
  <p>  
    <%= f.label :username %><br />  
    <%= f.text_field :username %>  
  </p>  
  <%= f.fields_for :radcheck do |builder| %>  
    <li>  
      <%= builder.label :attribute_name %>
      <%= builder.text_field :attribute_name %>  
    </li>  
  <% end %>  
  <p><%= f.submit "Submit" %></p>  
<% end %>

When I tried to save with attribute name other than User-Password, Expiration, Simultaneous-Use, it is giving

1 error prohibited this raduser from being saved:

 - Attribute name is not included in the list

If you want to change the message, you can add :message to the validations. You can add other validations like this in the Radcheck model.

See these links RailsCasts, Complex form codes

Sign up to request clarification or add additional context in comments.

2 Comments

What about if I need to validate the actual content of the fields, like checking for a date?
@rubyprice - am trying to validate the fields but it simply doesn't work. Have followed your instructions to the letter and been through the tutorials online. Any suggestions how I can validate the attribute_name field?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.