I want to achieve a has_many association with a x number of records, and the records would be named.
Let's explain that better. In a previous question I asked how to make a text area with a selectable markup language and we reached the conclusion that I needed a separate model, Field which had the multiple fields I needed (language, original and rendered).
Now I want to be able to make a model, let's say User, which had two of these fields. For example: about_me and biography. How would I create those fields every time I create the user, edit them when I edit the user and destroy them when I edit the user? And how would I display them simply writing: User.about_me and User.biography?
Thanks in advance for the answer.
David's solution creates the joint model.
Then you have to include the form of the profile in the User form. You'll have to use accepts_nested_attributes_for method in the User model.
To destroy the profile when user is deleted, add dependent => :destroy to the relationship between the 2 models.
You'll need to use callbacks (http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html).
For example, in your User model, have a after_create callback that will create the requisite fields.
Also,have an after_save callback that checks user.changed? and if it is different, update the fields.
Related
User story: User can create a Template. Each template has_many questions. User has_many templates. Template and Question belongs_to user.
Right now the user can create questions for a template. User clicks on template and they see a list of questions.
Problem:
How do i turn this into a form where the user can answer the questions. The user needs to be able to use the template and infinite amount of times. A user must be able to click what template they want to use then fill out the form.
Thank you for any assistance.
Although your question is a bit vague, it sounds like you need to set up a couple tables to hold answers to your questions, which also belong to a user. I would recommend a table to hold a reference to all responses for a given template: i.e. belongs_to :user, :template. Then, create a table to hold answers to each question, maybe called answers? Each row would belong to a user and question and thus a given 'template' with a through: option set on a has_many association. Does that make sense?
I have a need to see when an user started a subscription. I need to add a new column named subscription_started_time in the users table created by a Devise generated migration.
I can easily add the column, but how do I populate it? I obviously shouldn't use strong parameters, because the time input will be added straight from the controller and it wont be coming through a form.
What would be the best way to handle input straight from Ruby when the user creates/updates an account?
If you want to set User's :subscription_started_time at it's creation, you can use one of ActiveRecords hooks - before_create.
Let's consider an example:
class User < ActiveRecord::Base
# ...
before_create do
self.subscription_started_time = DateTime.new
end
end
Please, check documentation for more hooks available for your ActiveRecord models.
Hope that helps!
I have one type of user creating form fields for a job posting. They can define the field type and name (eg. type:"string" and name:"job_title").
My question is how do I store this data so that the job form for the applicant user has all of these defined fields?
Currently the job and form template is organized like this:
job BELONGS TO employer_template
employer_template HAS MANY jobs
employer_template HAS MANY template_fields
I've tried to search for solutions, but haven't seen anything that answers it clearly enough.
I would avoid to create all those tables, it sounds an overengineered solution.
You could simply serialize the fields and the contents, and store them in just one field.
That is automatically done by serialize:
class employer < ActiveRecord::Base
serialize :custom_fields
end
Simple and fast :)
My comments are placed by users. The Comment belongs_to :user and the User has_many :comments.
But users can be removed. If done, I'd rather not delete their comments, but instead associate their comments with one Dummy user.
I can think of several ways:
On load of a comment, if no associated user is found, create a User in memory with dummy data.
On load of a comment, if no associated user is found, pick a predefined one from the database.
On removal of a comment, associate all the comments with a predefined User in the database; through some post filter.
My feeling says that number one is cleanest; since the other two require a user in the database that will be hardwired in the code. If user 18394 will be that "special" user, I'd need all kinds of safetynets for that special user.
What about soft deleting users instead? Have a boolean field called User.active and set a default scope for User.active = t. When a user gets deleted, set the active field to false, and clear out any personal data.
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.