I'm creating a app of which a unique user registers people entering a building.
The user takes the person's name, ID, plus a picture. When a person is registered,it updates a feed (like twitter's feed) that shows who entered the place (showing only the name and id). Each addition to the feed is actually a data model called "micropost". So each user has many microposts, and "micropost" belongs to "user".
The feed has to show all people who entered the building at a given day, both newly registered visitors and visitors registered long ago who just happened to pass by.
The issue is: i need that when the user clicks on visitor's name in the feed, it gets redirected to a profile page of that particular visitor, showing his/her complete info (name,ID and photo), plus the dates of all prior visits. I need to have many microposts redirecting to the same visitor profile, if they are showing information about the same person.
I'm trying to figure out the model relation between the user, microposts and visitors profiles. I don't know how it would work in this case.
The relation between user and micropost is easy to get, because users are unique. The problem is that microposts aren't, and as just said, even already registered visitors must be shown in the feed.
Thanks for any constructive feedback.
i think you are facing issue with microposts,visitors history
create table visitors(user_id,post_id, created_at)
1) When user visit post, insert a new record in the visitors table with the id of post, fetch record from visitor table to display previous user history.
in your models:
class User < ActiveRecord::Base
has_many :microposts
end
class Micropost < ActiveRecord::Base
belongs_to :user
end
in controller:
#user = User.find(:id)
in view:
<p>#user.name</p>
#user.microspots.order(:created_at).each do |mp|
<p>IN:<%=mp.created_at%></p>
end
I hope this help you.
Related
I guess not much help here. When you are trying to associate a user from a dropdown list, and imagine if you have 1000000 users, you would see the user instance. I need it to show the actual user's email address.
app/models/user.rb:
has_one :company
app/models/company.rb:
belongs_to :user
The link I was given has nothing to do with the dropdown's value.
Have I setup my rails association incorrectly? Funny thing was, using rails admin I had no issue in this department as I could associate a company when creating a user but not so with active admin.
All I want is when I select the User dropdown, as in picture, I'd see a list of user email addresses.
Tim was correct all this time. I needed to create a function in the User model.
app/models/user.rb:
def display_name
email
end
Rails newbie: I currently have a basic app where Customers(users) have many points (customer model and points model). And I want an admin user (new model) to have the ability to add points to the customer.
-The customer enters their phone number (#index route).
-If the customer is not found, they will be brought to a signup page (#new/#create route).
-If customer is found in the database, their profile will show (#show route).
Now on this page, I want to be able to have an admin passcode, which once entered, gives access to adding points. I also want to keep track of which admin user, gives which customer points on a different page.
How would the schema look like with the admin user? How would I give it access certain access to features like adding points. (I assume I'm going to have to create a helper method for checking if admin is logged in, and keep track of that somehow, maybe with sessions?)
class Customer < ActiveRecord::Base
has_many :points
end
class Point < ActiveRecord::Base
belongs_to :customer
end
class Admin < ActiveRecord::Base
#??? (my best guess is has_many :points, has_many :customers)
end
www.loyalty-app.herokuapp.com
I think the best way is to not have a special admin class and just make admins a type of customer (or user). Then use a gem like cancan or access granted (I prefer access granted) to handle what the different types of users can do.
How can I implement the same feature as Linkedin (count users who has viewed my profile page) for my rails app, so users will get notified for users who has viewed their profile?
You should add a new model, e.g.:
class PageView
belongs_to :page
belongs_to :user
end
Use it like this:
PageView.create(page: #page, user: current_user) # to register page view
PageView.where(page: #page).count # to get views count for particular page
PageView.where(page: #page, created_at: 1.week.ago..Time.now) # to get views count for particular page for a week
First of all you need to define your Visit model. It will have at least the following attributes:
id (visit id)
user_id (id of the visitor)
page_url (the page visited)
created_at (date and time of the visit)
Then you can decide to store it in a Relationa Database like MySQL or in a NO-SQL one like MongoDB. For this case any of them it could be a good option.
If you decide the SQL solution, just add a has_many :visits in your User model and :belongs_to :user in your Visit model.
And that's a good point to start from.
class Company < ActiveRecord::Base
has_many :users
end
The user hits on Company->Users href link and assuming it shows 3 users for a certain company.
I would want to set user.age (an integer) for all the users and press save. How do I do this in my controller / view code?
We usually have an Edit link against each User to modify its details, but I would like to set age information for all users.
Use a form_tag to submit to an arbitrary controller action set up specifically for editing multiple users (not one of your user scaffold methods). Then, add one fields_for tag per user inside of the form you created, each with a field for the age attribute. When submitted, you can simply iterate over the user params in the controller to manipulate your users.
I have two models, referral and user model.
Referral belongs_to User AND user has_one Referral
When a user signs up, they are given a unique referral, like this localhost:3000/absc
where absc is the unique referral Id.
When another users visits, localhost:3000/absc, using the routes.rb I have extracted the Id as follow
match '/:ref_token' => 'user#new'
#ref_token = params[:ref_token] #in users_controller.rb
Now I have a hits column in Referral, that should increment the value for referral id absc, for each visit to the URL.
How can I do so elegantly.
This may seem trivial, but I'm still on my path to Ruby Enlightenment, and I seek your wisdom.
In your controller, simply do :
Referall.find_by_ref_token(params[:ref_token]).increment!(:hits)