How Can I implement this in Rails? - ruby-on-rails

I want to implement a feature where user can "add additional details" to his profile. Here he should be able to create the label for the detail and the actual details like:
Education : Degree
where Education is the label for the detail and Degree is the detail.
Apart from this, he should also have an option to decide whether this details should be made visible or hidden.
How can I implement this using a new model Profile with the association User has_one Profile.
If I just had Label and Text for the new details, I could have tried hash, but since I would also have to get the details from the user on whether the user wants the detail to be made hidden or visible, I might require an extra field to store that value (true or false).
I am really confused as to how I can get the whole thing implemented together.
Please suggest me how I can implement this and also how can I update the model each time a user creates a new detail without changing the schema of the db.
I am working on Rails 3.2.

I don't really see your problem (or I don't get it), but:
Why don't you just create a has_many AdditionalData with user_id:integer name:string content:string visible:boolean?
So you can loop through #user.additional_datas.visible (assuming you defined a scope scope :visible, where("visible = 1").
Added benefit: You could make this model polymorph and add additional information to other things you have in your app without the need to create an extra "information" table for every model you want to store these.
For validation, you could also add a data_type field and create validations according to the data type (needs to be an url, a phone number, just text, ...)

I don't see the problem too; I think an hash-like table could be the solution, something like this:
rails generate model UserDetails label:string value:string \
visible:boolean user:references
in which you put records like this:
user.details.create(:label => 'Dog name', :value => 'Fuffy' :visible => false)

Related

Ruby on Rails adding relationship doesn't update new form

Basic rails question.
I have a class, let's say User, and another class, Category.
I set up a relationship between them, so that Users have categories and categories have users. When I go to users/new to create a new user though, there is no prompt for category. I'm expecting the rails magic to kick in and have done that, so I'm wondering if my expectations were wrong or if I haven't executed something properly.
You should set your User model to belong_to :category, and the Category to have_many :users.
That way, when you want to create a new user, it will ask you what category the user belongs to. You will probably need to run a migration to add the category_id index to the users table though. That will solve your problem.

Add 'current_user' to a model. (Devise) Rails

Hi I'm new here and also new in rails.
I want to add a couple values by default to a database called books (Model: Book.erb)
there is a user who creates these books(current_user), and I thought that a way to identify who creates and deletes this content is by adding some default values from the user and clasificate them (to be specific username and password)
my table ":books" has available two fields for adding username and password
I tried to do this:
# Book.erb
class Book < ActiveRecord::Base
after_save :set_default_values
def set_default_values
self.username = current_user.username
self.password = current_user.encrypted_password
end
end
but it seems to be that I can't call 'current_user' from this model.
I was reading a pratice on this blog
but some were saying that this method violates the MVC pattern, do you agree with them?
do you guys know a better way to do this process without violating the pattern?
Well, I'm not sure I can conceive of why you'd want to store a user name and user password in a book table as even if it was easily explained, it would be in violation of normalization practices for good database design which pretty much states you should only express a field once and then share it where it needs to be shared.
Now, assuming you must do this for some reason I can't conceive, I'd have to ask if "username" is your actual field or is it just "name" which is more standard Rails. And, I believe you'll have to have a relationship between these models to pull the data from one into the other and I don't see that book has_many users or one or belongs_to or anything of that sort.
With a relationship between book and user you have access to all user properties without writing them anywhere other than the user table. So I think you probably want to look at that.

Rails beginner help with making a custom order form

So what I basically would like to do is make an order form. In this order form, users will submit information about their company members, and there will also be a part at the end of the order form where the user can select option services that are added by my client from a backend. I am new to rails so I would just like someone to help me make sure that I am going to do this using the best possible practice. Here is what I think I need to do:
Make an Order model
Make a Member model
Make a Field model
In my order model make
has_many :members
has_many :fields
In my member model make belongs_to :order and in my field model make belongs_to :order. Then what I need to do in my orders controller is #fields = Field.all and extract it in a #fields.each block.
P.S. there is one other thing I have to do and that is to make the order form displays 3 types of headers: Corporation, LLC, and Non-profit. What I thought would be smart is if in the url I made it like type=1 and type=2 and type=3 and in my model use an if statement like:
def order_type(type)
if type === "1"
"corporation"
elsif type === "2"
"llc"
else
"nonprofit"
end
end
I think using an if statement is kinda sloppy so for that so if someone could please explain to me the best practice that would be great. Please don't try and answer with a railscasts episode because I have checked out a lot of them already. What I would like is an explanation if possible
Thanks guys it means a lot
Are your fields really so complicated that they require their own model? If your "fields" are a one column list just include them as a column in the model they are applied too.
The problem with using the URL method that you suggested is as follows. Say a user creates a model object and assigns it as a "LLC.". Then your URL would have type=1 somewhere in it as you suggested. Now a user bookmarks this URL and afterwards someone realized it was a typo and instead of LLC the heading should be something else. Now you have a unRESTful situation where you are creating dead links on the Internet or you have urls that are encoded. URL encoding should be used for querying data not dceciding what the data should be.
I would not mess with the urls for the heading question. Instead just add a heading field to the main model that will be displayed and have that field either be LLC etc. Then in the view just have a variable that loads in the value for the object being displayed.

what is the right way to model status values as an association in Rails?

I have a model called Contacts.
Contacts can have different status "bad, positive, wrong..."
These status may need to be changed over time, but across all contacts, they are the same options.
Should I model it this way:
Contacts.rb
belongs_to :status_contact
StatusContacts.rb
has_many :contacts
Then I manually populate the types of status in the table?
I then want to use Ajax to click a button corresponding to a value to update the value for Contacts.
It looks like you're trying to ensure that the values for your status are always going to restricted to a set of possible answers of your choosing. If that's all you're trying to do, there's no special need for a separate table. You can use the magic of ActiveRecord validations instead here.
First, create a string database column for Contact called :status.
Then you can use a validation to ensure that the values are limited to the ones you want. In Rails 3, you can do it like this:
validate :status, :inclusion => { :in => %w( bad positive wrong ) }
(If you're using Rails 2, use #validates_inclusion_of instead.)
In ActiveRecord, validations check that the object's values are valid before saving; it refuses to persist the object into the database until all validations pass.
Your naming strikes me as a little weird—ContactStatus sounds a little nicer to me—but I see this as being the general idea to achieve what you want.
No clear answer yet --- I think I need to use the table because it would allow the users to add and modify the types of status used across their application.

Ruby on Rails - Optional Associations?

I would like to allow users to write comments on a site. If they are registered users their username is displayed with the comment, otherwise allow them to type in a name which is displayed instead.
I was going to create a default anonymous user in the database and link every non-registered comment to that user. Would there be a better way to do it?
Any advice appreciated.
Thanks.
The problem with creating an anonymous user is then you need to check if a comment was made by a "real" user, or an anonymous one when displaying the name, so that introduces complexity. Plus, if you have a way of viewing their profile page, which may include posting history, you'd need to exclude the anonymous user with an exception.
Generally it's better to have a column on your comments which represents the user's visible name, and just show that if provided, or the registered user's name otherwise. For instance, your view helper might look like this:
class Comment < ActiveRecord::Base
belongs_to :user
def user_name
self.anonymous_name or (self.user and self.user.name) or 'Anonymous'
end
end
This will display the contents of the anonymous_name field of the Comment record, or the user's name if a user is assigned, or 'Anonymous' as a last-ditch effort to show something.
Sometimes it's advantageous to actually de-normalize a lot of the database when dealing with large numbers of comments so you don't have to load in the user table via a join simply to display a name. Populating this field with the user's name, even if they're not anonymous, may help with this, though it does mean these values need to be updated when a username changes, presuming that's even possible.
I think you can make user_id on your comment model nullable since you want to allow non registered users to add comments as well. As far as adding names for the non registered users are concerned, there are two options for that
option 1. Add a column on Comment model and name it like anonymous_user where you will store names of non registered users
option 2. Create a another model AnonymousCommentor with name and comment_id attributes.
If you are going to use anonymous users for other things as well apart from comment in your application then you can make it polymorphic and use a suitable name like AnonymousUser instead of AnonymousCommentor

Resources