Id's in rails application - ruby-on-rails

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

Related

Rails Nested Form and dependent relationships

I currently have a few models, users, clients, addresses and contacts.
I used scaffold to create my client model which only contains name, leadsource and DBA. It is currently working just fine.
My address and contact was created separately as a client can have many addresses and contacts and they require a client_id to set up the relationship.
Assuming I am using your standard crud operations, is there any way to set up a single nested form that will allow me to add all 3 at once?
Sure, the standard way is fields_for.
But normally you also want to dynamically add and remove associated objects (i.e. addresses) and that's a bit more advanced.
There are gems to help you. i.e. cocoon and a great railscast: episode 403.
Unfortunately it's a pro episode, but investing the $9 is well worth it, as the railscasts really are a source of inspiration (at least for me).
I assume, you already have the associations or create them:
rails generate migration AddClientReferenceToAddress client:references
rake db:migrate
and in models:
class Client < ActiveRecord::Base
has_many :addresses
end
class Address < ActiveRecord::Base
belongs_to :client
end

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 : Model with Array of object of my class

I'm beginning RoR. I've designed my model like this :
User
-login:string
-password:string
-email:string
-followers:array (type user)
I have now this termainal rails command :*
rails generate model User login:string password:string email:string
but i don't know how to tell my generated model that I would like an array of User.
I think my question is a bit stupid because Ruby is like PHP (no type). But I prefer to ask ...
Thanks for your help !
If you want to have something like followers you have to use a many-to-many association.
Take a look at associations in the rails guide : http://guides.rubyonrails.org/association_basics.html
You have to remember that when you generate a model and you specify login:string for example you specify the name and the type of the column which will be created in your database.
The right way is to have a many-to-many relationship. You have to say that your User has_and_belongs_to_many followers (I assume if a Use has many followers he can follow many User?). You will need to create an other table which will associate a user to another.
You'll find many articles on google which explain how to create many to many relationship.
But RailsGuides are really well done, take a look at it first.
Edit:
As your follower will be also be of type User, you'll have to do something like that:
has_and_belongs_to_many :followers, :class_name => "User"
You can take a look at the documentation for other options:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_and_belongs_to_many

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.

Routing Users to single Models with Rails

I'm creating a Rails app for students and high schools and I'm having some trouble with my User.rb.
I want to have a user model to be used for logging in, but having that user have many roles. The tricky part is that I want users that have a student role to have_one student page, and those that have a role of principal to have_one high_school page.
The students and also nested in the high_school so the entire thing becomes a big mess.
So my question(s): How do I limit a user to only creating one student / high school to represent them? Also how would I nest this student pages inside the highschool without screwing up the user system?
My environment: Rails3 and Ruby 1.9.2dev
Thank you!
Follow up: Would it be possible to put the name of the high_school in the subdomain? That would make the url look like
highschoolname.mysite.com/students/eric-koslow
I'd suggest polymorphic association to user_representations. It'd hold info about which high_school object or which student_page to associate the appropriate user to.
You can made a validation to avoid the multi-creation.

Resources