I want to design a registration form for new customer. The rails way I know is to create a "customer" model with all attributes of customer and then write a controller and html.erb. The challenge I have is that the attributes of a customer are in multiple DB tables. I need a way to create a model that is joint of multiple tables and when I commit, it saves to multiple tables. any tips will be appreciated.
Extracting your model into multiple models is the simplest way to do this. For example, if your database has 'Customer', 'Address' and 'Location' tables create a 'Customer' model that has one 'Address' and has one 'Location'. Then in your form, use a nested model form for creating and updating. This tutorial is a good starting place:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
Related
I've had this question about ruby on rails some time ago, and managed to solve it in some way:
The solution I've found is use nesting forms with the join model(fields_for), saving the third model within an external form and inserting its ID with ajax.
The problem is that the nested form is too big to nest inside Order form(main form).
I'm not satisfied with this solution, so here it is:
Let's say I have three models:
Order
Order-List (has_many through model)
List
So, the Order contains different Lists, and each List has Items (With other has_many relationships between).
In an example, know that I can save the information about the has_many through model on the same form, but how do I save an Order AND a new List(on the same form)?
Thanks in advance!
I am modelling a Trainer/Member relationship on Ruby on Rails and I was wondering what is the best way to model this relationship.
Currently I only defined roles for a User class using Rolify for view and controller access.
Trainers and Members are users.
Should I do a recursive relation between the User model, or should I create a Trainer and a Member model specifically on Rails, and set up hierarchy between them, and create the relationship between the child models?
I would setup two different models for trainers and members as you proposed and use the rails association helpers. For example, if a member can have several trainers and trainers can have many members you would want to implement a many-to-many relationship. This is most easily done in rails via the "has_many :through" model helpers. You can then add an attribute to these classes that would specify the levels of authorization or controller access on your app if need be.
I'm very new to Rails so sorry in advance if this is a fairly obvious question.
I need to make a form that can create records for multiple models in Rails. My models are categories of grocery-store items: Produce, Meat, Dairy, etc.
In my create form, I want the user to be able to create a product by selecting which model it belongs to and then have the controller insert the record into the appropriate DB table.
so my questions are:
should I still define each model as a resource?
is it possible to use one controller to add, update, delete for all of the product models?
Have you considered just making a category column in your items model? If not you could use active record associations to accomplish something like this. See the documentation at:
http://guides.rubyonrails.org/association_basics.html
New Rails guy here...
I have a SimpleForm model which belongs to a Parts table belonging to another schema that's not Rails application.
What's the best way to model this association so that my form can do a lookup of the Parts table for part_id and the model can validate the part_id foreign key against the legacy Parts table?
possibilities:
create a database view and activerecord model in rails app?
create a readonly model with query/connection to Parts table?
Please be specific, as I really don't know much within Rails.
I am using Rails with Oracle and Windows, so any solution has to work with these.
Thanks in advance!
I ended up using database migration to create a database view and created readonly model for it.
In my SimpleForm, setup belongs_to relationship to the new Parts model and validates_presence_of to enforce the foreign constraint on inserts and edits.
Looks like this is pretty good way to do readonly foreign key reference by granting only "select" permission to legacy table and also setting Rails model to readonly.
I have a user table, and a teacher that I newly created. The teacher is sub class of user, so, I use scaffold generator to generate the teacher table, than, I modify the model to do teacher is subclass of user. After all that, I did a db:migrate. Then, I go to
http://localhost:3000/teachers/new
It shows an error:
undefined method `teacherSalary' for #<Teacher:0x103331900>
So, my question is what did I do wrong? I want to create a page for doing user register, the user can ONLY be a teacher / student. But I can't add a teacher record ... ... Moreover, I go to
http://localhost:3000/users/new
I want to have a combo box that allow user register their user to be a "teacher" or a "student". But everything seems not work like I expected. What I need to do? Thank you very very much for your help.
Within your database you should have a single table called users. This table should have a string column which by default is called type. If you use another name for this column then you will have to set the inheritance column name manually using self.inheritance_column = "column_name"
Within your application you have three models, User, Student and Teacher. User inherits from ActiveRecord::Base as usual, Student and Teacher both inherit from User.
You should then be able to instantiate new Teacher and Student objects. Internally this works by writing the model name to the type field on the user tables and then when you use Student.find it adds a clause to the SQL to only return rows where the type = 'Student'
You can add shared behaviour to the User class, e.g. validations etc then add additional behaviour to the inherited classes.
A fuller description of how STI works can be found in Martin Fowlers Book(Patterns of Enterprise Application Architecture).
I found this definition really handy:
STI means one table contains the data of more than one model, usually differentiated by the "type" column. ("users" table contains data for the models "Teacher", ""Pupil", "Employee", "Assistant", etc.)
Keeps similar models in the same table instead of creating new ones.
A Polymorphic Association means that one model can be associated with more than one other model(Comment can belong to post, image, file, user_type...)
To prevent foreign key conflicts, the association is reperesented with the *_id and *_type columns instead of only *_id.
For what you have here , I am not sure if STI is the best way go . STI should generally be used when there is a OO like inheritance and the Models have the same Attribute but different behaviour . In your case Teacher and Student can sure have a few shared attributed , but they are also bound to have different ones as well .
You might want to experiment with a polymorphic association as well .