Model relationship in rails and their handling - ruby-on-rails

I have a requirement for an app where two models invoice and message needs to be linked. The link/relationship should be able to do following things:
The invoice should be able to store the message_id.
The message should also be able to store the invoice_id - a foreign key to the invoice table.
There are some extra fields in both models.
Also can you tell me how will i be able to generate a form_for for this kind of models, where two models get data at the same time but of different fields. Should i use hidden_fields?
Please Help.

Here You will learn about has_one and belong_to associations, which can be used with table structure You are searching for (though without extra explanation it sounds odd).
http://guides.rubyonrails.org/association_basics.html#the-belongs-to-association
http://guides.rubyonrails.org/association_basics.html#the-has-one-association
After linking You models check nested attributes Railscasts to get and idea how You can construct form for multiple elements with mass-assignment supported:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2

Related

Ruby on Rails Model Relationships

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

Rails blog and post linking

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.

In a single form, linking two instances of two models with habtm

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

Id's in rails application

In my application I have a user model and a address model. How do I have the address form to include the id of the user id. (i.e my address model has a id and a id_user)?
Your address model should contain a user_id field, not a id_user field. It seems like you're just getting started with Rails, so I would recommend reading the official Getting Started and Association basics guides to learn the basics.
You probably want to look into record associations. I strongly recommend the guide on that very subject on the Rails website.
Since you took the time of creating two separate models (one for users and one for addresses) I figure it's because you can have one or more adresse per user. So I would recommend using a has_many association
In your migration file for your address model you should have a column name user_id of type integer. By convention, rails will use columns named model_id for an association with a model. So in your case, the model user can be linked with an address through the column user_id.
Having the column is not enough however. You also need to tell Rails about the association in the model. So add the following to the top of your user model (app/model/user.rb I would presume)
has_many :addresses
Then, tell your address model it belongs to a user. Doing so is again very straight forward. Add the following at the top of your address model (app/model/address.rb)
belongs_to :user
From there, you can do all sort of wonderful things. To create your forms, you may want to look at Nested model forms. There's two Railscast on it Part 1 and Part 2
As your experience with Rails seems limited at the time. I'd recommend also looking into the getting started guide, looking at the series of tutorial from Envy Labs (comically named Rails for Zombies) and when you get rolling browse through the Railscasts by Ryans Bates

Multiple non-related models in one Rails form

I am building a blog-style application in Rails 3 where multiple users are able to post some news. After the login (which is realized with "Authlogic") the user values are stored in a own model called e.g. "UserSession". The form for the post contains title, content etc. and the username should be stored with a hidden form.
I think that the two models don't need to be related to each other (by that I mean a :has_many - :belongs_to relationship) because there isn't any further usage for that information.
Do I really not need this relation? And how could I realize the form?
For Authlogic is it important to remember that the 'UserSession' does not correspond to any database tables (i.e. you would never use a has_many or has_one 'UserSession'). I think the relationship you are looking for is:
User has many Posts
Blog belongs to User
The reason? It is always a good idea to associate a record with the 'owner' so that the owner can later modify or delete the record. I hope this helps.

Resources