Multiple non-related models in one Rails form - ruby-on-rails

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.

Related

Allowing admin and users to sign in using same form (rails)

I've created 2 tables, one for users and one for admins.
I created 2 tables as they both collect different information, but I want to be able to allow a sign in using an email address and password from both the admin and user tables via the same form.
Is this possible? I've looked around and people seem to have created 1 users table and added an admin boolean, but I wanted to avoid this and I didn't want to collect unnecessary data if I didn't need to.
Any help and assistance about how to best go around this would be great.
If you are implementing something from scratch, then it is simply a matter of coding it. I think this approach has some inherent flaws and I would avoid it.
If you want to have some segregation on the model side of things, I suggest you use STI. That way there is some shared behaviour/attributes and the distinctions can be coded separately, so you have your protection.
If you have plenty of distinct attributes, I would suggest separating them from your user/admin and creating an "admin_profile" model that belongs_to :admin and a "user_profile" that belongs_to :user.
And to make coding "transparent", you can create accessors in your admin model class to get/set the profile attributes seamlessly. Say you have an is_cool attribute on the admin_profile model, but you'd like to access it as
imadmin.is_cool
You can have in your admin.rb model
has_one :admin_profile
def is_cool
self.admin_profile.is_cool
end
be careful cause the has_one relationship may return nil if there is no profile associated with the admin/user.

Add email model that allows for one primary and multiple secondary email addresses

In Rails, I want to create a model email that allows for polymorphic associations from different people, say 'users' and 'admins' so that they 'has_one' primary_email and 'has_many' secondary_emails.
Is there a way to do this under one model called Email?
I was thinking of creating an Email model that has a column for primary_email:text and another column secondary_email:array (of emails).
I feel like there is a better way to do this though.
EDIT
Now I am thinking of creating a simple Email model with just one email column (and emailable_id and emailable_type columns as well). Then User will "has_many" emails. Now I am a little confused as to how the relations work. Right now a user already has an email column. Can anyone think of a way to consider this their primary email in the User model. Then if the user already has a primary email, create a secondary email.
To be clear
I want all of the emails to be part of the Email model, and the distinction between primary and secondary emails to be made in the User model.
Can anyone think of a way to do something like that?
Thanks!
If I were to do this, I would go for an array attribute using the serialize method. (See this post and this tut.)
Remember that has_many is an association between two models, so it is definitely not possible to use it with only one model. But there is also nothing wrong with creating a second model for storing only one email, and using has_many

In a belongs_to has_many relationship in Rails, how does the 'singlular' side access records from the 'many' side?

It was my understanding that all keys are stored on the 'many' side, ie: if a user has many posts, and posts belong to only one user, foreign keys associating which user "owns" which which post exist only in the post object.
Yet, in my users controller, I can still create a user show page which lists all posts by looping through 'user.posts'.
How is this possible?
Thanks in advance.
Edit: one caveat, I'm using MongoDB / Mongoid, not sure how this pans out with Active Record.
The model with belongs_to part holds the column for reference, user_id in your situation.
The model with has_many part queries the table, where belongs_to is, to get all references models.
Hope it helped you!

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

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