Data modelling of group and users - data-structures

I have a web app that lets users create groups.
On the front page of the app any user can - without registering - create a group and add other users to the group by entering names into a list of text inputs. The user that created the group is then supposed to be able to invite the people on the list by adding an email to each of the names. The reason I want the user to provide just names initially and ivitations by email later is that I want the user to be able to try out the app and its functionality before registration.
So I have two models Group and User with a many-to-many relationship. The user model requires a unique email address so obviously I can't create new user objects for each user in the group. So how would one make sure that the temporary users added to the groups will be mapped to the users that are created through the invitation or if the invited person already has a user in the app?
I was thinking about creating a Profile model that would have a name and belong to a group and a user but that way each user would have a profile for each of the groups he/she belongs to. I can't think of another alternative but there must be one...

You need tools to dump dat info from your brain to a database model :)
http://wb.mysql.com/ or http://www.sequelpro.com/
Then you need to abstract that into your application.
http://datamapper.org/
After install all datamapper gems
gem install dm-core dm-do-adapter dm-migrations dm-sqlite-adapter dm-timestamps do_sqlite3
And finally you can start playing. In your Sinatra app.
require 'rubygems'
require 'dm-core' # http://datamapper.org/getting-started
configure :development do
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/hohohodb.db")
end
#...
class User
include DataMapper::Resource
property :id, Serial # http://datamapper.org/docs/properties
property :username, String
property :email, String
property :created_at, DateTime
property :updated_at, DateTime
validates_uniqueness_of, :email # http://datamapper.org/docs/validations
has n, :groups
end
# Create or Upgrade all tables at once
DataMapper.auto_upgrade!
get '/'
erb :index
end
__END__
##index
...
Hope this can be a good starting point # http://datamapper.org/docs/
have fun

Related

How to avoid multiple users page acess at same time and same record in rails?

Would like to display a message when click any product edit page if somebody
already opened/in that same record/page.
how to track users access on edit action for same record?ultimately i want to display message as "somebody already editing this product" to avoid overwrite
process between multiple users.
please share your ideas on it.
Naive solution for this might be adding an association say:
class Product < ActiveRecord::Base
belongs_to :edited_by, class_name: 'User', foreign_key: 'edited_by_user_id'
end
class User < ActiveRecord::Base
has_many :edits, class_name: 'Product', foreign_key: 'edited_by_user_id'
end
and then:
First comes on /edit page, set product.edited_by to that user.
Second user visits /edit page. You can check if edited_by for that product is set, then show him the message; blocking him to update the same product.
Remove the edited_by when user has updated the record.
But this comes at cost. There are lot of corner cases around this:
User might come on edit page and acquire edited_by association. But never update the record (thus never giving another user to update the product)
In case of some logic exception, edited_by might never be reset.
Involves devising a strategy when to reset if case #1 occurs.
Thus, I advise using Optimistic Locking instead. You can add a migration with :lock_version for your Product model. This will prevent the stale object from ever being saved in this scenario, preventing such conflicts.
It will raise the exception ActiveRecord::StaleObjectError when User #2(or first) tries to update an old record, which you can use to show your custom message by handling that exeption.
Hope that helps :)

Rails nested model not saving correctly

Using Ruby 1.8.7, Rails 3.2.13, SimpleForm and ActiveRecord. I have a form for a Contact with an optional Volunteer (a contact can optionally "be" a volunteer, the relationship is one-to-one).
Editing of a contact is currently working fine, the volunteer fields are displayed and saved correctly.
However, when creating a new contact, I get an error message: Volunteer contact This field is required.
I'm creating the new contact (and its volunteer) like this: #model = Contact.new(model)
And contact allows the setting of the volunteer attributes:
class Contact < BaseModel
has_one :volunteer
attr_accessible :volunteer_attributes
accepts_nested_attributes_for :volunteer
end
So it seems the Volunteer's contact is not pointing back to the new contact that is created? Am I missing something here?

Rails - How to use the 'has_secure_password' technique for emails

Rails 3.1.0 - Ruby 1.8.6
I'm trying to create a signup page. I came across this 'has_secure_password' with basically does all the magic for you to enter your password twice and checks if they are correct.
I'd like to use this technique to check on email address as well. If it's not possible, what is the easiest way for me to get the user to key in their email twice and check if they match?
Please help. I'm a rails noob.
Thanks
Rails has a validates_confirmation_of validation built in. If your model has
class User < ActiveRecord::Base
attr_accessor :email_confirmation
validates_confirmation_of :email
end
Then you just need an email confirmation text field in your form and rails will check that they match.

How do I create an admin in Ruby On Rails using the Devise Gem?

I am developing a rails application and am using a gem called devise to manage my users. I have created a new user called "Admin" but am unsure on how to change a user on the application from a "User" to an "Admin".
On the documentation it says:
"The code below can be used to grant admin status to the current user."
current_user.update_attribute :admin, true
But where would this snippet go?
Here is the documentation, The admin role creation info is near the bottom of the page.
https://github.com/plataformatec/devise/wiki/How-To%3A-Add-an-Admin-Role
You are very close to the solution! Nothing like reading through the documentation :-)
By going with Option 2 mentioned on the wikipage, users in your application will be classified
as 'regular' or 'admin', based on the admin attribute. The wikipage gives you the code for granting admin role to the current user, and leaves the decision of where to call this code up to you.
Fair enough, since how users become Admins is specific to each application, depending on how the users want it to be done.
One way to do it would be to have a 'Grant Current User Admin Rights' action in the GUI which would invoke the code. In that case, the code would go within a 'grant_current_user_admin_rights' method in the 'users_controller.rb' file. Of course, the views and the routes should be modified accordingly as well.
You could call that code from after_create callback on the user model, ensuring all users become Admins :-)
Another way to do it would be to set the admin flag for specific users either in the console or through database seeds.
Example from a seeds file on one of my projects:
admin_user = User.new( :email => USER_EMAIL, :password => PASSWORD_STRING, :name => USER_NAME )
admin_user.admin = true
admin_user.save!
Hope this helps.
4 years late but the true answer to OPs question, when he asked where to put:
current_user.update_attribute :admin, true
can be solved by going to the terminal/command prompt.
Type in
rails c
to access the rails terminal.
then go to your user, and since you're probably the first it'll be:
user = User.find(1)
user.update_attribute(:admin, true)
Assuming you followed all the previous steps in Option 2 of the documentation, this will set your user to have a true Admin attribute.
You can verify this by going
User.find(1)
and it should say "admin: true" at the end of the big block of text.

How can I work with attributes of a belongs_to relationship in Rails 3.1?

I have a couple of basic Rails problems that I'm having trouble finding relevant current (Rails 3+) information on. Here is the first:
How do I access attributes of a parent object to display them in the view? I have the following models:
class Site < ActiveRecord::Base
has_many :devices
class Device < ActiveRecord::Base
belongs_to :site
I'm using regular restful routes (do nested resources come into play here?) and the standard find methods in the Devices controller. In the view for devices I want to display the name of the site that owns the device, but everything I try gives me errors. How can I access and display the Site.name value for a given device?
Thanks in advance for your help!
It's hard to know what's wrong without seeing what you're trying. Compare the working example below with your technique. But first check that your device actually has a site, as explored here: Rails belongs_to association, can't access owner's attributes when part of a collection?
Try this in your rails console:
site = Site.create(:name => "Boston")
device = Device.create(:name => "hackatron")
site.devices << device
device.site.name #=> "Boston"
You can see my full output in this gist
If that doesn't help pinpoint where your error is, please share some code and the errors you're seeing.

Resources