I am in the process of creating an application with Rails, what I am trying to do is have each user, upon registration, select 1 country, and 1 state within the country they are associated with. I know I can create classes like Country, and State and use belongs_to, has_many associations. But how would I be able to list all countries/states in a form and how would I map together a specific country/state name to that one user?
Thanks in advance!
Sounds like a job for Carmen.
No need to reinvent the wheel; it supplies the lists of countries and subregions so you don't have to manage them. The documentation also includes sample code for displaying the country-appropriate subregions (states) via JavaScript.
grouped select, example specifies exactly what you need:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-grouped_collection_select
Related
We have a users table. Users have many listings.
We'd like to shard the association model Listing such that all users stay on database "master" shard. Users will get a shard_id column and listings will be split into different databases "shard1", "shard2".
We can augment our code to access the listings on the correct shard using the using method:
Listing.where(user: current_user).using(current_user.shard_id)
However that is a big code change. Ideally we want to just keep using our existing association statements like this:
current_user.listings
And have it automatically use current_user.shard_id beneath the hood.
Any suggestions for doing this?
According to the documentation, current_user.listings should work out of the box.
Octopus also handles associations. When you try to get a object that is associated to another object, you could use normal ActiveRecord syntax to get the objects
https://github.com/thiagopradi/octopus/wiki/How-Octopus-Works
I don't know what's the best way to solve this problem and I'll appreciate your help.
This is what I want to achieve:
There is an Act model. And it has many Organizations.
An Organization can have many Groups and also, a Group can have many Subgroups.
The Groups are loaded dynamically depending on the selected Organization, and the Subgroups are also loaded depending on the selected Group.
An Organization could have a Group or not, and also a Group could have a Subgroupor not
Here you are a mock-up of what I have explained above:
In the new Act form there is a section to add Organizations, something like this:
When you write an Organization it loads the options for the Group selector, and the same happens with a selected Group and the Subgroup selector:
When you click the add button, a new Organization section appears
A field can be empty. (This is a valid form)
Also, If you want to edit the Act, the form should show the Organizations added before:
I have this DB:
ActOrganizations is a polymorphic table, and also a join table
My main problems are about the form: how to build it to retrieve and persist the data easily and how to retrieve the stored Organizations to show them in the edit form. (I'm using simple-form)
I tried many approaches, but they were not very elegant. I'd like to know the closest way to the "rails-way" to do this.
Thank you very much for your time
I have three models which are User, Domain and Association. My user model is populated from devise. My Domain model consists of "People's interests such as - running, biking etc". My association table links a user_id to a domain_id and stores it as a record. When I add a domain it automatically populates the user sign up form with domain's (in which a user can pick e.g. I like running).
My Query is.. What active record query could I use to pull user's with the similar interests e.g. running, with the currently logged on user. So if a user called Tom was logged in and had select domain interests of running and biking (linked to), how could I pull back a database object with other users that had the similar domain's (interests)
So basically if Tom, Paul and Steph were users, but Tom and Paul selected the same domain's and Steph selected different ones, what would be the Query code to do this? Hope this is enough information.
I think you could do something like this:
User.joins(:domains).where("domains.id IN (?)", current_user.domains.pluck(:domain_id))
current_user.domains.pluck(:domain_id) collects all the domain ids that the current_user has selected. Pluck is only in rails 3.2, if you're using an earlier version, you'll have to use collect instead.
Then the first part of the query selects the users who also have that domain as one of their interests.
You should look at the rails guide as Bongs suggests
http://guides.rubyonrails.org/association_basics.html
I have two models, Book and Chapter.
I would like chapters to be accessed by their order in the book rather that their id's
/Books/the-chapter-title/chapters/1
Would it be recommended to do this as I would like to also have the ability to reorder the chapters.
I would like the chapter numbers to move up and down the list using acts-as-list but also access them via their pecking order.
you can add a to_params method in your model if you want to use something other than id, but this usually leads to headaches
check out https://github.com/norman/friendly_id
I'm building an app called "CourseWork to dig into rails/develop my skills and I have a question about how to structure it. Users have a resource called "CourseGrading" that is able to create categories and belongs to "Course". Each "category" should have a name, a percentage out of 100 and a course_id. I need to add these percentages together and alert users if the total isn't 100 while still saving.
Then the user's generated "categories" should populate an enum_string specific to that user in a resource called "CourseAssignment" which has a name, description, category and finalgrade.
Can anyone give hints or resources for how best to accomplish this? Thanks
You probably want to take a look at Active Record Callbacks. These will allow you to insert some code to be run when creating/validating/updating/deleting models.
You should probably make use of the ActiveRecord validations.
Check out this guide that explains how to write your own custom validator. Your custom validator would run when the form gets submitted, and in it, you would grab the percentage params and do your check. If it's not what you expect, you can just add an error to the form and the validation process will just kick the user back to the form page and display the error.