Hi: Problem in storing two different Model Values in one Table

0 views
Skip to first unread message

Puneet Pandey

unread,
May 19, 2008, 9:25:48 AM5/19/08
to BANGALORE RUG-Ruby Users Group
Hi Everyone,

Hope You all are good and doing well. Guys I am here to discuss one of my problem that I am facing while using two diferent models to store values in one table.

Here I am describing you my problem Hope anyone can help me out.

I have one model 'faculty.rb', and a controller 'faculties_controller.rb', Using this controller the values are stored in 'facuties' table.

Means, 'faculties_controller' ---> (Including Fields Validation of form) accessing ---> 'faculty.rb' (MODEL) ---> (After Saving, Values are going into) ---> 'faculties' TABLE

Now I am creating another controller, i.e 'faculty_admin_controller.rb' (with ruby script/generate controller faculty_admin new create edit update), I have not created its model (i.e without generating script/generate model command, I am creating a Model Manually), Say 'faculty_admin.rb' (in app/models).

The Problem arises here, that, I want both these two models should access same table i.e 'faculties' TABLE. Actually this whole thing is a Signup Process but for two different Users. For the First User Some Values are going to be stored in 'faculties' TABLE, while for the second user the values will also store in the same table, but those two users have different models and different controller.

Now what I have done till now,

app/controller/faculties_controller.rb

def new
    @faculties = Faculty.new
end

def create
    @faculties = Faculty.new(params[:faculty])
    @faculties.save
    "some action"
end

in app/model/faculty.rb

class Faculty < ActiveRecord::Base
**** Fields Validations *****
end

in app/views/faculties/new.rhtml
<% form_tag %>
****** Number of Fields That we want to store*******
<% end %>

Now on the second hand,

app/controller/faculty_admin_controller.rb

def new
    @faculties = Faculty.new
end

def create
    @faculties = Faculty.new(params[:faculty])
    @faculties.save
end

in app/models/faculty_admin.rb (which I have created Manually)

class FacultyAdmin < ActiveRecord::Observer
    def after_create(faculty_admin)
        FacultyNotifier.validates_adminfaculty(@faculty)
    end
end

in app/models/faculty_notifier.rb

class FacultyNotifier < ActiveRecord::Base
    def adminfaculty
        validates_presence_of :email
        attr_protected :hashed_password, :enabled
        attr_accessor :password
    end
end

Now, Please Anyone can tell me where I am wrong?? Please Reply. I will be very thankful to You.

--
___________________

Puneet Pandey
Senior Software Engineer
Safew Technology Labs
pun...@safewlabs.com
http://www.safewlabs.com
http://puneetitengineer.wordpress.com

Satish Kota

unread,
May 20, 2008, 1:21:10 AM5/20/08
to bangal...@googlegroups.com
Hi Puneeth,
 
Use the attached files. I have created 2 models Faculy and FacultyAdmin. Faculty directly connects to faculties table as it is the plural of faculty. However for FacultyAdmin to read data from the same table, you have to set the table name within faculty admin.
 
Use the below code for your working
 
class FacultyAdmin < ActiveRecord::Base
  def self.table_name()
    "faculties"
  end
end
 
Now both Faculty and Faculty_Admin takes data from the same table and updates also.

Thanks and Regards
Satish N Kota

 
Reply all
Reply to author
Forward
0 new messages