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
Related
I have a rails app. Users can create products that will be listed on products index page (including some data about the user who posted it) and everybody can see the list on app/products.html.
What is the best way to implement this? Should I do it with nested resources (user has many products) in which case I can use product.user.name for displaying the user name or should I create an independent class so when user creates a product, some user attributes (name, etc.) will get saved in the product table.
Your mixing together quite a few different concepts here.
Nested routes
In REST you have a concept of nested resources which is expressed though URIs such as:
posts/:post_id/comments # comment that belong to a resource.
Which tells us that there is a "has many" relation between post and comments.
The best practice here is that:
Don't nest if you don't need to.
Never nest more than 1 level deep. posts/:post_id/comments/:comment_id/replies for example should be comments/:comment_id/replies.
Associations and domain modeling
Domain modeling on the other hand is how your models fit together. In ActiveRecord each model class is backed by a database table.
Each model should correspond to a single type of object in your problem domain. So in your case you would have a User class and Products class.
They would be linked by a products.user_id column. So no - you should not store users attributes in the products table.
I'm looking for a solution to use many to many association in active model serializer.
Let's say I have a user with many user types through a many to many table, how can I return the user types for a specific user?
Do I need to create a serializer for the many to many model?
The answer was pretty stupid. I had set the serializer to "has_one user_type", a simple change to "has_many user_types" worked.
I have an article model article.rb (with related images):
attr_accessible :name, :content, :image_ids, :altname1
I now want to create another kind of articles, that are just for cities. They will appear in a completely different section of the website and are not related to all other articles. But the model is exactly the same.
Is it better to create a new model cityarticle.rb or is it better to add a new boolean column (cityarticle with true and false as options) into the existing article model. I'd then add a new action to the controller:
def cityarticles
#cityarticles = Article.where("cityarticle = ?", true)
end
For me it's easier to keep just one model, but there might be good reasons for a new model? How about performance?
Some questions to ask yourself: In what other ways will these types of articles be different? Will different people have access to create/edit/delete them? How do I create a city article and not let it accidentally be saved as a non-city article, or vice versa? Am I using attr_accessible or strong_parameters to prevent users from overriding the article type with params passed in? Will the different types have different validation rules (maybe requiring a city name for city articles)?
How hard will it be to get just the articles I want? How likely am I to forget to specify what kind of articles to show and show all of them?
Based on answers to questions like those, you should be able to decide what data structure will work best for you. If you use Rails' STI pattern, you could wind up with one table but two different models, as one solution. Performance is probably not the main consideration, but if the two types of articles are never going to be queried together, it might be slightly better in terms of performance to split them into separate tables.
New model represents a new "entity" on the System.
Say CityArticles extends Article
It should be a new Model for code clarity and extensibility to increment functionality over the "CityArticles".
You can make a new table scaffold or migration:
rails g scaffold CityArticles references:article
Or making Article class polimorfic association. Read http://teachmetocode.com/articles/ruby-on-rails-what-are-polymorphic-associations/
This question is related to: How to link form after creating rails join table
I Had a products model, with a categories field, however needed each category in individual rows rather than the comma separated rows they were in. So I created a Category and ProductCategory model, and added all appropriate associations.
How do I link up the categories field from the old products model to the new Category table so that when a user enter a new product and adds the category, it will save to the category table.
There is a great Railscast on setting up accepts_nested_attributes_for it should be just what you are looking for. I would do you a better service by just telling you to watch it, rather than replicate code.
http://railscasts.com/episodes/196-nested-model-form-part-1
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