I currently have a routes file as
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :users do
resources :posts
end
match "posts" => "posts#main"
get "home/index"
root :to => 'home#index'
end
What I would like to do now is, also show /users/:user_id/problems/:id as /problems/:id. But not sure how should I put it into routes?
Note: I do have a relationship between users and posts.
class User < ActiveRecord::Base
has_many :posts
and
class Post < ActiveRecord::Base
belongs_to :user
Thanks
UDPATE: The error I get is Couldn't find User without an ID
You need to use shallow routes in following way,
resources :users, :shallow => true do |user|
user.resources :posts
end
Related
In my app, user is used for the authentication phase, and with a polymorphic has_one association, it will be associated at different type of users, with different actions.
This is the user model:
class User < ApplicationRecord
has_secure_password
validates :username, presence: true,
uniqueness: true
belongs_to :role, :polymorphic => true, dependent: :destroy
end
and this is one of the models associated
class Guest < ApplicationRecord
has_one :user, as: :role
end
Logging and authentication call home_index_path, and the current user is stored in current_user.
In the Home Controller i have:
def index
if current_user
redirect_to current_user.role
else
render 'unlogged'
end
end
In route.rb i have:
resource :guest do
member do
get 'dashboard'
end
end
resolve ('Guest') {[:guest]}
Now the problem: assuming that the user is a guest, in this way i'm redirected to method show of GuestsController, but i need that is redirected to method dashboard.
How can i do?
Ok, i've resolved with polymorphing_path
redirect_to polymorphic_path([:dashboard, current_user.role])
I have some AR models
User
has_many :clients, :through => :users_to_clients
has_many :files
Client
has_many :users_to_clients
has_many :users, :through => :users_to_clients
has_many :files
File
belongs_to :client
belongs_to :user
and try to fetch all files, trough clients assigned to user
u = User.includes(:clients => :xls_files).find(1)
This code fire 3 sql query. In final sql looks like what i need he fecth all files trough clients on user.
SELECT "files".* FROM "files" WHERE "files"."client_id" IN (1, 2)
but how to get this data, if u variable just contain User object?
Try this
User
has_many :files
has_many :clients, :through => :files
Client
has_many :files
has_many :users, :through => :files
File
belongs_to :users
belongs_to :clients
Then you can do stuff like #user.clients and #client.users
I can't tell from your post, but you may have to switch the client and file models around, so client belongs to users and files, depending on your situation.
I don't find any solution so i made it by hand by adding scope in File model
scope :by_user_clients, lambda { |user| where(client_id:
user.clients.pluck(:id)) }
File.by_user_clients(#user)
I have routes like this:
namespace :admin do
resources :users, :only => :index do
resources :skills, :only => :index
end
end
resources :skills
In this case I got:
admin_user_skills GET /admin/users/:user_id/skills(.:format)
{:action=>"index", :controller=>"admin/skills"}
How to change nested route in order to point to SkillsController instead of Admin::SkillsController? I'd like to have this:
admin_user_skills GET /admin/users/:user_id/skills(.:format)
{:action=>"index", :controller=>"skills"}
Interesting thing - if we have no Admin::SkillsController, it will use SkillsController automatically, but only in development.
Using namespace in routes implies to have special directory for "namespaced" controllers, admin in your case. But if you use scope instead you have what you need:
scope '/admin' do
resources :users, :only => :index do
resources :skills, :only => :index
end
end
I need to query all posts from a specific user and include all comments and the user who belongs to the comment.
class User < ...
has_many :posts
has_many :comments
end
class Post < ...
belongs_to :user
has_many :comments
end
class Comment < ...
belongs_to :user
belongs_to :post
end
#posts = current_user.posts.include(:comments)
Is is possible to also get the comment user? I list a lot of posts and comments. I do not want to query each comment user.
Thx / Tobias
Try
#posts = current_user.posts.includes( :comments => :user)
Read more about it here
How about include at the relation definition statement?
:include
Specify second-order associations that should be eager loaded when this object is loaded.
class Post <
belongs_to :user
has_many :comments, :include => [:user], :limit => 5
end
I have the following:
class Org < ActiveRecord::Base
has_many :users
has_many :entries
end
class Entry < ActiveRecord::Base
belongs_to :org
belongs_to :user
validates_presence_of :entry_text
end
class User < ActiveRecord::Base
belongs_to :org
has_many :entries
validates_uniqueness_of :user_name
validates_presence_of :user_name, :length => { :minimum => 3 }
end
I can Create Orgs and Users... How do i create an entry if there are two belongs_to? and what is this pattern called?
Double nested resources are tricky. The trick with users usually is to keep it out of your desired entry path.
Your question is kind of broad, but if you specify more information, people would be able to help you better. Also, I would recommend using the gem Devise for your user management system. Since you're using 'users' I would assume you want users from orgs to create entries. The entry created would be a part of org and the user would be the session's current user. Sorry if I am wrong to assume this.
Your routes.rb file can look something like this (assuming rails 3):
resources :orgs do
resources :entries
end
Then the create of your entry controller would look like:
#entry = #org.entries.new(params[:topic])
#entry.user = current_user #or however you are managing the current user's session.
And you'd want to set the org for the entire class by making a method that loads your current org and do a before_filter :loadOrg
def loadOrg
#org = Org.find(params[:id])
end
This is of course assuming your path is something like: /org/(id)/entry/(entry_id)
and not
/org/(id)/user/(user_id)/entry/(entry_id)
which in my opinion is unnecessary and can lead to more problems. You can always create a userpage model that calls all entries by users, but the default route doesn't necessarily have to include users in the path.
I don't see any problem.
#entry = Entry.create(:entry_text => "Hello World!")
Now questions to clarify what do you need:
Can #entry belongs both org and user at the same time? Or it can belongs to only one of them?
Should #entry belongs to at least one of them?
If #entry supposed to belong only one of them, so you should use Polymorphism
http://railscasts.com/episodes/154-polymorphic-association
class Entry < ActiveRecord::Base
belongs_to :textable, :polymorphic => true
validates_presence_of :entry_text
end
class Org < ActiveRecord::Base
has_many :users
has_many :entries, :as => :textable
end
class User < ActiveRecord::Base
belongs_to :org
has_many :entries, :as => :textable
validates_uniqueness_of :user_name
validates_presence_of :user_name, :length => { :minimum => 3 }
end