Rails : Model with Array of object of my class - ruby-on-rails

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

Related

Is such association possible in rails?

I have a List model and a User model. A List belongs to a User and also can be followed by many Users. How do I make association so that I can do such operations:
list.user - gives me the user that created(belongs to) the list.
list.followers - gives me a list of users following the list.
user.following - gives me all the lists followed by a user.
can someone please help me create such associations in rails as well as how do I create migrations for this association.
First of all generate a user model.
Then for list model, give migration like:
$rails g model list user:references follower_id:integer followee_id:integer
then $rake db:migrate
Now in list.rb
validates_uniqueness_of :followee_id, scope::follower_id
Hope it helps
Edit
so for full code do refer my blog https://additionalknowledge.wordpress.com/2017/08/06/making-follow-mapping-on-rails/#more-559 follow each step don't miss.

Are associations necessary in both model and table?

I'm rather new to RoR and have been going through the associations guide provided at rubyonrails.org.
I've noticed though in each example, when it gives what "the corresponding migration might look like", they've included the association in the migration table.
I've been including associations in my models, is it necessary for me to add them within the migration tables to? Or is the guide providing this to help readers understand the association?
For the record, I do understand if an association is made between two objects, each object model needs to have an id to associate with.
Thanks in advance!
Kristian,
Associations can get a little confusing.
You only need to add them in your models. For example if I have a post model with a user.
Post.rb (model)
belongs_to :users
User.rb
has_many :posts
Here is a link that may help you
http://www.tutorialspoint.com/ruby-on-rails/rails-models.html
Here is a railscast video that might help:
http://railscasts.com/episodes/154-polymorphic-association?view=asciicast
PS: You do not need to add them to migrations. Migrations are there to rebuild the database if you need to restart or rollback

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

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

How to create a view to manage associations between HABTM models? (Rails)

I am using Ruby on Rails and need to create a view that allows the creation of records through a HABTM relationship to another model. Specifically, I have the following models: Customer and ServiceOverride, and a join table customers_serviceoverrides. Using the customer view for create/update, I need to be able to create, update and delete ServiceOverrides and manage the attributes of the associated model(s) from the same view.
Visually I'd prefer to have something like a plus/minus sign to add/delete service overrides, and each serviceoverride record has two string entities which need to be displayed and editable as well. However, if I could just get the code (a kind of nested form, I'm assuming?) working, I could work out the UI aspects.
The models are pretty simple:
class ServiceOverride < ActiveRecord::Base
has_and_belongs_to_many :customers
end
class Customer < ActiveRecord::Base
has_and_belongs_to_many :serviceoverrides
end
The closest thing I've found explaining this online is on this blog but it doesn't really address what I'm trying to do (both manage the linkages to the other model, and edit attributes of that model.
Any help is appreciated. Thanks in advance.
Chris
The ascii cast at http://asciicasts.com/episodes/17-habtm-checkboxes has a simple and functional example.

Resources