Im having trouble to show the photos from a my Usermodel.
I want to show the photos ONLY from the users that don't have role assigned with rolify.
So, I need to create a query for it:
#photos = Photo.where(User.find_by(Role == nil))
I can't get the scope to work...
Can someone give it a try?
Thanks!
To get all users with no role:
User.with_role(nil)
So, assuming that Photo belongs_to :user (photos table should have a user_id column):
#photos = Photo.where(user_id: User.with_role(nil))
Related
I have two ActiveRecords Users and Posts:
class User < ActiveRecord::Base
has_many :post
end
class Post < MyBase
belongs_to :user
end
I'm trying to get list of all users and all their posts.
However when running the following code, I get the users who has posts with id 1 or 2, but only these posts. I want to get these users with all their posts.
Any ideas how to do that?
users = User.sorted.references([:posts]).includes([:posts]).where('posts.id=1' OR posts.id=2).all
This is one way to do it, first get the user ids of posts with id 1 or 2, and then get all those users. There are other ways of course, but this at least should work for you.
user_ids = Post.where('id = 1 OR id = 2').pluck(:user_id)
users = User.where(id: user_ids)
I have a User model and a Photo model. If I go into Heroku Console, I can search the database, for example:
u = User.find(2)
u.photos.count
=> 25
I want to create a scope in my User model so that I can sort the users based on their photos.count number and then paginate the users.
Class User
scope :photocount ??????
UsersController
def index
#users = User.photocount.paginate(page: params[:page])
end
What you want is counter_cache. Add a column photos_count to user model and in Photo model:
belongs_to :user, counter_cache:true
Then you can User.order(:photos_count).page(params[:page])
See more about counter cache in rails guides here
Scope will look like:
scope :by_photos_count, ->{ order(:photos_count) }
Without counter cache it is still possible, but will be very inefficient:
User.joins('join (select user_id, count(*) as sort from photos group by user_id) as sort ON users.id=sort.user_id').order('sort.sort').page(params[:page])
I fully admit this is user inexperience but here is my problem
I have 2 models that relate by
class User < ActiveRecord::Base
belongs_to :team
class Team < ActiveRecord::Base
has_many :users
What I want to do is display all users that belong to the same team as the logged in user. so basically select from user there team_id = my team_id
I was not sure if I could / should do this in the controller or the view but I could not get either to work.
in my controller I have this which returns all users for all teams
#users = User
which is SELECT users.* FROM users
I can also use my current_user method which returns info on my current user
#users = current_user.team
which is SELECT teams.* FROM teams WHERE teams.id = 3
I dont know how to get a list of all users where team_id = current_user team_id?
Also would like to know if its best to try this in the controller or out in the view?
Thanks
As suggested by #apneadiving using curent_user.team.users would return the list of users. But I just wanted to advise you to wrap that behavior in a method.
So for instance you could have in your User model something like :
class User < ActiveRecord::Base
belongs_to :team
def coworkers
team.users
end
end
And then in your controller you could do
def an_action
#coworkers = current_user.coworkers
end
Finally in your view you could loop through this list to display them
#coworkers.each do |coworker|
...
lets say I have three Models
User(id, guest(boolean))
belongs_to :room_user
Room (id)
RoomUser (id, room_id, user_id)
has_many :users
Right now I can do, room.room_users and get back all the associated users that are in that room. That's very doable with rails.
What I want to do now is something like Room.room_users_active
So in the Room model I have:
def room_users_active
self.room_users.where(:......)
end
The challenge here is I want the condition to reach into the user table. And do the following:
Return all the room_users where the user is not a guest (User.guest == false).
Ideas? Thanks
Not tested, but it should work, try and let me know
def room_users_active
room_users.joins(:users).where('user.guest = false')
end
Fixed a wrong method call see here
I've got a User model that has many Items. A Rating belongs to a User and an Item.
In the DB, I have set ratings.user_id to be not NULL.
when I am creating an Item, I would like to do this:
def create
current_user.items.create(params[:item]).ratings.create(params[:rating]
redirect_to items_path
end
However, this balks with an SQL error "user_id cannot be nil"
so I rewrote the create method as
def create
current_user.items.create(params[:item]).ratings.create(params[:rating].merge({:user_id => current_user}))
redirect_to items_path
end
which works fine.
However, I had thought that chaining the create methods off the current user's receiver would have populated the rating's user_id. Anyone know why not?
TIA.
I'd recommend you normalize this if possible in the database. Maybe take out the user_id attribute from the ratings table and if you need it in your model get it through a join using a :through method
class Rating
has_many :items
has_one :user, :through=>:items
If you created and saved the Item, then made a Rating from that item, it wouldn't pass the user along to the Rating, right? You'd just refer to it as #rating.item.user, right?
When you think about it like that, you wouldn't expect the Item created via the current_user to pass the user information along to the rating.
Make me wonder if you really need the user has_many ratings relationship.
Because Item has many Ratings and that association does not know about the user id. Given that association chain Item would have a user id because it belongs to a user. And Rating would have an item id because it belongs to an item. But the Item to Rating assocation doesn't know anything about a user unless you tell it.