I am building an ecommerce app in rails and would like some help on my model relationships and how i get the tables populated with the information.
I have a user model, an order model and an address model. I want to capture the address in a form during the order process.
In my order model i have user_id, pay_type, email, address_id and name.
In my address I have the user_id, (then some address fields).
the relationships are
order belongs_to a user
address belongs_to a user
address belongs_to an order
order has_one address
user has_many orders
user has_one address
So when a customer clicks checkout I want them to enter the name thats on the Order, email, address and paytype. The user_id will be captured using currently signed in user method through a hidden field. (If there is a better way let me know). sorry if this isn't explained very well
You'll need to look into accepts_nested_attributes and nesting forms.
Or just look through some code here, e.g.
order model
order form
order controller
The main things here to note are:
accepts_nested_attributes_for
f.fields_for :address do |address| will be needed to render the nested form
#order.save in the controller will save all nested data
Related
i'm new to rails and your help and advise would be much appreciated as i am finding this challenging
Aim: i want the creator of the event to be able to select more than one user as
hosts for a created event (just like how facebook allows the creator of
a page to be be able to select users as admins of a created page). Is the below how my model and schema should be displayed?
i was aiming to build something like this image. Event1 i can select Ian & Jesse as hosts, Event2 i can also select Ian again as a host and select Emma
This is how i imagine it so far to be built (your guidance would be much appreciated):
models
user.rb
has_many events
event.rb
belongs_to user
host.rb
belongs_to user
has_many events
schema
users
name
email
events
title
address
user_id
hosts
user_id
event_id
Started writing this as a comment but realised it was getting too wordy.
your model is broken ... an event has many users .. it doesn't belong_to a single user.
What you have is a many to many relationship between users and events which needs resolving through a join table (aka associative/junction table). You have gone some way to resolving this with the hosts table though this goes against the rails convention.
What you want is something like:
models
user.rb
has_and_belongs_to_many :events
event.rb
has_and_belongs_to_many :users
and create a join table that references the two models
users table
name
email
events table
title
address
events_hosts table
user_id
event_id
The rails convention is for the join table to be named by joining the two names of the tables it is joining lexically ordered - i.e. events before hosts, concatenated together to give events_hosts.
Alternatively, you can also create a join model if you prefer:
EventHost
belongs_to :user
belongs_to :event
and modify the has_and_belongs_to_many to has_many :event_hosts in the other two models - the database schema will remain the same.
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
I have a Boat Model and its Models such as Brand, Model and Year. I have also User model and I would like to connect them by adding migrations to User model of boat_id and I added belongs_to :boat and has_many :boats to User model. But I can not reach User.first.boat.name from the console even though I am able to reach Boat.first.brand.name.
When I try User.first.boat.name. Console gives an error saying;
NoMethodError: undefined method `boat' for #<User:0x0000000665dc30>
Btw: Boat Model includes model_id brand_id and year_id.
EDIT1:
Or should i remove Boat model and add model_id brand_id and year_id to User model directly.
EDIT2:
I would like to be able to reach User.first.boat.brand.name or User.first.boat.year.nameor User.first.boat.model.name
EDIT3:
Every boat has one brand, year and model. But user can have many boats
EDIT4:
What i will do is;
User can sign up and login
Then User press the link list my boat.
He/she saves the boat then the page renders to User Profile
In the User profile I do not know how to get current user boat name year etc. That is why I am confused. Sorry for the misunderstanding
I think you're confused about how Rails associations work in conjunction with how they are stored in the database. If a User can have many boats, then the foreign key needs to be on the boats table. Currently you have boat_id in the users table, this should be removed and a user_id column needs to be added to the boats table as per Matt's answer.
Reference
To achieve what you're trying to do, you'll need to setup your models in the following manner:
class User
has_many :boats
...
end
class Boat
belongs_to :user # table has a user_id column
...
end
Then you can access a boat's brand using user.boats.first.brand.name
Run rails generate migration, then fill in the change method as follows:
def change
add_column :boats, :user_id, :integer
end
Then run rake db:migrate.
You user model has_many boats, so you need the boats table to refer to users. It's probably worth reading the Rails guide for ActiveRecord associations to get a better feel for how this works: http://guides.rubyonrails.org/association_basics.html#the-has-many-association
I'm making a Rails app with a Contact resource, with address, state, and zip fields. There's also fields for users to enter a phone number. On a form from another website that I'm using for inspiration, users can indicate whether the phone is mobile, home or office, in other words the type of phone number. I'm wondering if it's possible to create fields that accept more information about other fields on a model, or if, in this case, Phone should be a separate model (for example, Contact has_many :phones) and the type of phone number as a regular field on the Phone model.
Because in my app a User has_one Contact, I'd rather keep all the phone related information in the Contact model, rather than have User has_one Contact, and Contact has_many :phones.
Using another model is an option to achieve this. Another one is to create a Hash storing the phone numbers.
class Contact
attr_accessible :phones
serialize :phones, Hash
end
And then you could store each phone in it's appropriate key, e.g.
contact = Contact.new
contact.phones = {home: '1234-1234', work: '1234-5678', mobile: '9876-5432'}
contact.save!
And they would be accessible by the phones Hash:
contact.phone[:home] # => "1234-1234"
By providing the serialize command on the model, ActiveRecord serializes it in order to store on the database.
So it allows you to store Arrays or Hashes to the database, provided you create a text field on its table.
The migration would be:
rails g migration add_phones_to_contact phones:text
For more about serialization: api.rubyonrails.org/classes/ActiveRecord/Base.html
I have a "company" resource. When the user signs up for a free account they only have to enter an email address. When they wish to upgrade their account, i need to take additional details from them such as their address. How best to model this?
Store the address details in the company table?
Have a separate "address" table with one to one mapping with companies, and upgrade
would be on the address new action?
Create an action on the company resource that upgrades account, and takes the address info?
Create a new resource called "upgrade" which adds the additional address info
into the company table?
thanks
"premium" should be separated from "being/representing a company". I'd go with a separate Company model, with enough fields for address etc.
A Company will then belong to :user, and a User will have one :company.
See the adr microformat (http://microformats.org/wiki/adr) if you need hints on which fields to use for addresses.
I could see you having a separate model for Address or just storing it on Company. Personally I would probably choose having a separate model because in any given system it's likely you'll have Addresses associated with multiple models. Another benefit is that it helps separate out address-related responsibilities from Company.
I would set up your validations like this:
Class Company
has_one :address
validates_presence_of :address, :if => :upgraded?
validates_presence_of :email
...
private
def upgraded?
<true if user has upgraded, else false>
end
end