Rails nested model not saving correctly - ruby

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?

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 - 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 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.

Rails - how to update model without required items

I have a model that contains required and free fields. Between the required items is also item with password.
In the _form.html.erb I hid the input with a password, so when the user want to update the form, so he will be update the all items except password => that's what I want. But because the item password is require, so I will get an error about bad validations.
I would like to ask you, if exist a good way to do this... I can't find some trick for it
I think it's :
validates_presence_of :password, :on => :create

Data modelling of group and users

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

Resources