I am new to rails so go easy. I have two tables that I am trying to work with here, 'post' and 'category'.
The 'post' table includes the following columns, title:string content:text category:string.
The 'category' table simply contains name:string.
The idea is that the client can manage the categories and also when adding a new post, they can select from a drop down that references their categories.
What is the best way to accomplish this?
You might want to model the category differently. The usual approach is to create a PostCategory model and controller, and use a relation from posts to PostCategory. Read up on belongs_to and the other rails associations before you get much further into this project. When you're ready to continue, take a look at formtastic, it makes handling the forms for the associations much easier to code
flyfishr64 is right, the "correct" way to do this would be to put the categories in their own model/table.
There's lots of helpers like collection_select that will take your list of categories (PostCategory.all) and make a dropdown list for you with the appropriate name to save it in a specific field.
That said, you could pull a distinct list of the entries in that column already and use that for your dropdown, but it's a lot more hassle than just making a model for the category.
Related
My issue is simple, lets say I have two models/tables named 'abc' and 'pqr', both has three columns as 'a','b','c' in abc and 'p','q','r' in pqr. This two models may or may not be related/nested.
what I want to do is to create a single webpage. On that webpage I want to create a single form which will submit the data for two models/table with single button. May be I will create two form but I want only one submit button. How do solve this issue in ruby on rails.
As in rails we have one model per table.
You can only use accepts_nested_attributes_for if the two models are related. Otherwise, if the models are unrelated, see Anton's answer in rails: a single simple_form with two unrelated models? describing how to use the fields_for helper to accomplish this.
I can suggest you something in Ruby side. It is possible to do that with accepts_nested_attributes_for method.
You can add to
models/abc.rb
accepts_nested_attributes_for :pqr
You can find more information about it here.
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
I am very new to Ruby on Rails.
I am trying to set up a relationship between a user model and a model of ten different items.
My goal is to have users be able to check off items in the items model and then have the ones that have been checked off display on their profile.
I have used the Michael Hartl Ruby on Rails tutorial up to
the point of creating microposts.
Any tips on tutorials that will help me complete this would be greatly appreciated.
Thanks!
Basically, what you want is:
A User has_and_belongs_to_many :items
Also, an Item has_and_belongs_to_many :users
This is many to many relationship. Since, a user can has many items, and an item can belong to many users too. In rails, here has_and_belongs_to_many will implicitly create a table items_users which will contain id's of both, establishing the relationship.
Read more about this association here - http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association
Use checkbox tag for showing checkboxes for all the items. Documentation - http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box
Based on whatever checkboxes are checked, save the records, establishing the relationship.
Done. :)
I don't know about other tutorials, if you've completed Hatel's then you have a very very good understanding of the rails framework as a whole. I would have an items_list model. Which had a user_id foreign key to associate itself with a user. Then I could have an items model which had an items_list foreign key to associate them to a list. Then items model could have a boolean field "active" or "checked" or whatever. Using these, and the associated relations, and some scopes, you can get what you want.
Just make sure to use the includes helper when you request this data, otherwise you'll easily get a N+1 problem.
http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
I have a rails app that works almost like a blog, and I use a tagging system to categorize the posts.
I need to add to some of the posts something similar to a "related posts" feature.
So for example if post 1 is related to post 4, at the end of the show action for post one I want to render an image of post 4 and at the same time at the end of post 4 an image of post 1.
My idea is to create a "link" model that has a HABTM relations with the post model, but I'm not sure if a "post" has many "links" trough "linkings" would be better.
Both of the ideas seem to have the same result, so which approach should I prefer?
HABTM is by nature very simple, with just a table of foreign key pairs joining models.
Typically has_many through is used when you need to add additional attributes to that join relation, and/or when you need to treat the joins as their own model.
In your case, for example, you might want the links to appear in the order that they were created. For this to happen you'd need to store the create timestamp on the relationship. For this, the simple HABTM join table is not enough, so you switch to has_many through and create a Linking model to encapsulate the join.
To continue the example, you might also make Linking a first-class resource, and have a page where you can edit/add/remove them separately from either linked Post.
Personally I've always used has_many through in the majority of cases. It just feels cleaner to me (no auto-naming table magic to accept or override, and the linking is more visible), and I find that very often, join relationships do deserve to be first class citizens.
I am using Rails 3.
I have a Product model and a Group model (a group has_many users, through membership).
I would like to build the new.html.erb form for the product model, and at the end of the form, I would like the user to be able to choose members from which group(s) can have access to the product he wants to add.
So, my goal is to list the groups to which the user belongs to, adding a checkbox for each of them. Then, create the associations between the product inserted and the different groups the user selected when the form is submitted, but I really do not understand how to achieve this, as all the documentation I have read use the BUILD or CREATE method that defines a new instance of group, instead of an existing one.
Is it possible with a nested form, and a HABTM relationship between product and group ? Or should I use a nested form with a has_many_through association using new model product_group_relationship ? Or should I use something else than a nested form ?
I'm quite new in Rails and a little bit lost here, so if some experienced guy could guide me a little bit, it would be very much appreciated!
The form_for helper comes with a nice package of extra methods like: fields_for wich makes you able to add nested attributes for has_many_through relations.
I suggest reading these:
http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for
And make sure you set your model validations accordingly
Is it possible to use select fields with nested object forms feature of Rails 2.3?
Example:
Suppose you have a Article model, Category model, and a ArticleCategories join model. Article has_many Categories through ArticleCategories.
On our Edit Article form, you want to have an HTML select list of all the available categories. The user can select one or more Category names to assign to the Article (multiple select is enabled).
There are lots of ways to do this, but I'm wondering if there is a simple way to accomplish this using the nested objects feature. What would the form look like in your view?
Check out the nested form example from Github:
http://github.com/alloy/complex-form-examples
It's been a while since I looked at it, so I'm not sure if it covers exactly what you wanna do, but its a nice source for ideas / patterns.
Assuming you have defined the models and their relationships so you can do this:
#art = Article.find(article_id)
#art.categories # returns list of category objects this article is assigned to.
Then I usually use http://trendwork.kmf.de/175
You need to copy the JavaScript file into public/javascripts but after that you can just create the form element with something like:
swapselect(:article,#art,:categories,Category.find(:all).map { |cat| [cat.name, cat.id] })
(I would tend to wrap that in a helper to make the call even cleaner)
One small gotcha is that for very long lists it can run a little slow in IE6 because there's quite a lot of appendChild calls in the js which is notorioulsy slow in IE6
Update: Apologies. This doesn't really answer your original question, which was specifically about the Rails 2.3 feature. The swapselect option is version independent and doesn't make use of newer Rails functionality.