I have model Organization. It can be the user's company or it can be a client company.
So when creating an invoice, I need to set organization_id (the user's company) and a client_id (it's an id from the organization table, but now acting as a client).
How can I set this up? I still have no Client model setup.
Details added
Both the organization_id and the client_id are pulled from the Organizations table. So for example...
INVOICES
organization_id (this mean the invoice was created BY this
organization)
client_id (this means the invoice was created FOR that
organization)
But both of those ids are making reference to the same organizations table.
belongs_to :organization
belongs_to :client, :class_name => 'Organization'
Related
I have such models Supplier and Role.
Supplier should have one role, one role might belongs to multiple suppliers,
so I don't want to add foreign key to the Role model
I wan't to create all roles at the beginning and then add role to a supplier whenever I want
Is there a way to do that??
The foreign key in this case needs to go in Supplier. You can't put it in Role, if a Role can belong to many suppliers.
That makes this a belongs_to relationship by Rails convention, not a has_one.
Your Supplier needs a column called role_id and an association belongs_to :role. Your Role can then have a has_many :suppliers, if you want to be able to find all suppliers who use a given role.
I'm using Ruby on Rails 3.2.8 with Sorcery authentication.
What is the best way to create sub-users accounts?
Ideally, I would like to invite members by email and have them click on a link and they choose their username/password. Is it possible to do this within the same user table so they all can login from the same login page?
To wrap your head around what I'm trying to do is... Employer can add/invite employees to join the system and any data input to the system will belong to the employer.
Personally I would handle this as a single table inheritance model with a same-table association column.
Put a field called boss_id on your users table. You'll have the main User model, that has the Sorcery authentication stuff and any common logic. Underneath it you have Employer and Employee. They have a method called boss: for the employer, this returns self or nil (whichever you find makes most sense), whereas for employees, this is an actual association method, something like this:
class Employee < User
belongs_to :boss, class_name: 'User'
end
When an employer sends out an invitation, direct the invitees to a URL for invitations specific to the employer, like:
http://yoursite.com/employer/3/invitation
When the user creates their account you associate them to their owning employer.
When an employer views their data, ensure that you pull their child employees' data as well, using the users table as a join table:
class Employer < User
has_many :employees, class_name: 'User', foreign_key: 'boss_id'
has_many :contacts # Or whatever your application-specific stuff is
# that you want employers to see through their employees
has_many :employee_contacts, through: :employees, source: :contacts
end
If you need to assign ownership to employers in the database, I'd use an observer to watch for saves on the owned models, and if they are saved by someone with a boss, set an additional column to that boss' ID.
I've been watching Ryan's screencast:
http://railscasts.com/episodes/196-nested-model-form-part-1
I have a similar but different problem, and am hoping someone can help.
I have the following four models that are required (at minimum) to register:
Account
Company
Address
User
At registration, I'd like for Company, Address, and User to individually be associated with Account directly.
Also, I would like te User to be associate with the Company, and the Address to be associated with the Company.
Note: Each has a foreign key: account_id -- This is essentially a multi-tenant system with a single database.
My associates are currently setup as follows:
Account
has_many :companies
has_many :users
has_many :addresses
(In the future, other models will use the Company and the Address model, that is why Account has many of those)
User
belongs_to :account
belongs_to :company
Company
belongs_to :account
has_many :users
has_many :addresses
Address
belongs_to :account
belongs_to :company
I've been using accepts_nested_attributes_for method in the models, and the fields_for method in the views, but have only been able to get things associated in a purely nested way.
In other words, a User gets associate to a Company, but is not associated to an Account.
I need for each of the models to be associated with the Account. (Except for Account itself of course)
Is there a way to do this?
Thank you.
I think you confuse some where.
Account Has Many Companies
Company Has Many Users
Company belongs to account
account User belongs_to company
user belongs_to account either through company or create account_id in user
In views you can create form company and then create fields_for for account and inside account create fields_for user.
I think this will solve your purpose.
don't forget to add accept_nesetd_attributes inside company and account
I setup roles for the User model, for now, I just have a role string column for the users table. The value could be admin, salesperson, instructor or student.
Now I have a ScheduledSession model that an Instructor is supposed to teach. I have an instructor_id column, which is supposed to have an id from the users table (an user with a role of instructor).
So how can I setup the association in the models? in the User model is doesn't seem right to add has_many :scheduled_sessions because if the role is a salesperson or an admin, it just doesn't feel right. or setup something like belongs_to :user, :as instructor in the ScheduledSession model, but I get error Unknown key :as.
How would you set this up?
You want to set up your belongs_to slightly differently in the ScheduledSession model.
belongs_to :instructor, :class_name => 'User, :foreign_key => 'user_id'
This allows you to refer to the instructor of a ScheduledSession object and have it point to an entity in the User table.
There's no way around having a has_many relationship on your User model if you want to pursue a role based User with each user having a single role. Without it, you'll have no mechanism to retrieve the scheduled_sessions associated with a user.
An alternative might be to use Single Table inheritance here, which would allow you to create Admin users who don't have SchededSessions, but Instructors who do. You'll still store all of these in the same table, but ActiveRecord will make it easier for you to compartmentalize them.
I have a model called company and one called user, and User belongs to Company and Company has many Users.
But I want to store on the company model the master company admin user, but I want to do it with a custom name.
So, i want to do this: comapany.owner.name .
How can I do this in Rails 3?
your Company needs one additional field
owner_id :integer
then add to Company
belongs_to :owner, :class_name => "User"