ruby on rails - Add entry to table consisting only of parent model id -


i have 2 models in app: person , director; person has_one director. person can many things (officer, contractor etc , director); if director want store user id in directors table , nothing more.

the table set therefore consists of 4 columns:

mysql> show columns in directors; +------------+----------+------+-----+---------+----------------+ | field      | type     | null | key | default |          | +------------+----------+------+-----+---------+----------------+ | id         | int(11)  | no   | pri | null    | auto_increment | | person_id  | int(11)  | yes  | mul | null    |                | | created_at | datetime | yes  |     | null    |                | | updated_at | datetime | yes  |     | null    |                | +------------+----------+------+-----+---------+----------------+ 4 rows in set (0.03 sec) 

i each person entry on form have nested radio button creates entry in directors table if it's set "yes" , removes/doesn't create entry if it's set "no." seems simple i'm realizing have no idea how that, since explicit value radio button wouldn't saved database.

is there way pull off?

you can add attr_accessor person model, such is_director. temporary attribute not stored in database. , based on value of is_director, can have callback set logic make user own director.

class person < ...   ...    attr_accessor :is_director   after_create  :make_director  private    def make_director     if self.is_director       #your logic make user own director     else       #some other logic     end   end end 

then in form, can add radio buttons:

<%= f.label :is_director %><br /> <%= f.label :is_director, "yes", :value => "true"  %> <%= f.radio_button :is_director, true, :checked => is_director?(@person)  %> <%= f.label :is_director, "no", :value => "false" %> <%= f.radio_button :is_director, false, :checked => !is_director?(@person) %> 

then can create helper in persons_helper.rb:

def is_director?(person)   #whatever logic check if person director end 

you need add is_director permit array of strong params in controller.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -