Warning, I am new to ruby on rails. I know my database isn't setup all that great, but we're pulling in from a remote database and storing information from that database.
Users:
- id
- ...
stations
- id
- user_id
- hex_key (unique)
- ...
calls
- id
- reported by (hex key from stations)
- data source id (from remote database)
call details
- id
- call_index (data source id from calls)
responses
- id
- call_index (data source id from calls)
- response_id (from remote database)
response details
- id
- response_index (response_id from responses)
As far as the models go (this is all I have completed so far) I think this is also my biggest problem:
user has many stations, calls through stations and reports through calls
stations has many calls
calls has many responses and belongs to stations
response belongs to a call
I've been trying to figure this out, but how do i model this so i can get everything from the users correctly. something like this:
#user.responses.find(:all)
and that would give all the responses for that user
here is some information on joining tables.
http://www.ruby-forum.com/topic/64839
Based on the database schema and description here your models would look something like:
class User < ActiveRecord::Base
has_many :stations
end
class Station < ActiveRecord::Base
belongs_to :user
has_many :calls, :foreign_key => "reported by"
end
class Call < ActiveRecord::Base
has_many :call_details, :foreign_key => "call_index"
end
The difficulty here is that the database schema is not exactly what Rails expects by default (the convention). You will also probably need to specify the primary key for the tables if they are not using the ID.
Also, I am not sure you will be able to construct #user.responses because responses belong to calls, and calls belong to stations, and stations belong to users. This is effectively #user.stations.calls.responses.
Related
I have users who have certain relevant communications stored as associations.
Notes, Emails & Recordings
I'd like to run a single query which gathers all the relevant notes, emails and recordings ordered by date so that I can display them on a page.
I'm currently pulling them separately and sorting them ruby side but I'm finding that this isn't very efficient (I'm having to pull all records rather than just the date range for the 20 or so that will_paginate will return).
Is there a way of hacking this in SQL to achieve it? (It's slightly complicated by the fact that Email has a specific date that is not related to the database date.)
i.e. using an array I can get
#history = (#user.emails + #user.recordings + #user.notes)
#history = #history.sort_by {|record| (record.class == Email ? record.email_date : record.created_at)}.reverse!
but I'd like to try and pull that in an SQL query so that I end up with one response with the relevant items listed one record per row ordered by the relevant data fields, in the appropriate order.
I suggest using Single Table Inheritance for this. There is two ways to do this in your case. Which one to choose depends on the number of columns each 'communication type' has and how much they differ.
# user.rb
class User
has_many :communications
end
You create a communications table with a column type (string).
# communication.rb
class Communication < ActiveRecord::Base
belongs_to :user
end
Now you let your communications models inherit from Communication above:
# note.rb
class Note < Communication
end
# email.rb
class Email < Communication
end
# recording.rb
class Recording < Communication
end
The user model now automatically knows about the different communication types, so you can do things like this:
#current_user.notes.where(...)
#current_user.emails.find_by(...)
#current_user.communications.first
The simple (but for some people a bit cluttered way) is to put all necessary columns of all three models (notes, emails, communications) inside the communications table.
Another way is to move the details in another table:
# note.rb (all you need in the communications table is a `note_detail_id`)
class Note < Communication
has_one :note_detail
end
# note_detail.rb (this table carries all the note specific columns)
class NoteDetail < ActiveRecord::Base
belongs_to :note
end
And now finally - since you have all records in one table - you can do this:
#current_user.communications(order: :created_at)
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'm trying to set up a proper database-design, but I'm stuck.
Here is what I'm trying to save.
Every user can define a vote history list from imdb looking like this.
Two users can define the same list.
First I want to be able to save each list as an imdb_vote_history_list - list.
class ImdbVoteHistoryList < ActiveRecord::Base
has_and_belongs_to_many :vote_history_list
has_and_belongs_to_many :movies
# Fields
# id (Integer) - defined by the user
end
Each list should be unique and is being defined by it's ID (given in the link).
Each list has and belongs to many movies, as in the code above.
Each user should be able to pick a name for every list.
So instead of saying
Each imdb_vote_history_list belongs_to user
I create a new relation called vote_history_list.
class VoteHistoryList < ActiveRecord::Base
has_and_belongs_to_many :imdb_vote_history_lists
belongs_to :user
# Fields
# name (String)
end
Here the user can pick any name for the list, without interference with other user's names.
Is this a good way to store the data?
From the theoretical database design view this is the right approach.
For example the entity relationship model describes it this way. You can have relationships between entities and attributes at those relationships. If you map those to a relational model (database tables) you get a table for the relationship containing references to both entites and all additional information.
This is what theory can tell us about it :)
I am working on a Ruby on Rails 3 web application and am not sure how to relate two of the models.
In our organization sales reps go out on appointments. If the appointment is successful, it will result in creating an order (which then has the items ordered related to it, but that's for another day.) If this appointment is not successful, it will be marked as no sale and as you might have guessed, no order is created.
On the other hand, sometimes sales happen without an appointment. For example, a customer may call into the store and order something. In this case, an order can exist without an appointment.
It would be simple if there were no relationship between orders and appointments, but there has to be for ease of use for the end user. For example, if an appointment generates an order, but later the buyer cancels, they will mark the appointment as sale cancelled and then the system should automatically set the order as cancelled. Likewise,they may choose to cancel the order, then the appointment would have to be cancelled automatically by the system.
How does a developer handle something like this? Does the appointment :have_many => orders? does the order :belong_to => appointments? I don't know what to do!
Please help me with this, I am a pretty new rails developer and I feel in over my head! Thank you!
As you already said, the following will work fine:
class Appointment < ActiveRecord::Base
has_many :orders
end
class Order < ActiveRecord::Base
belongs_to :appointment
end
belongs_to requires the field appointment_id to be present in the orders table. But, if the order is not associated with an order then appointment_id does not need to be set. You can have multiple belongs_to associations for a given class.
My app is sending messages to client groups. I send message to each client in a loop.
I'm using 3 ActiveRecord models:
class Message < AbstractBase
has_and_belongs_to_many :groups
end
class Client < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :messages
has_and_belongs_to_many :clients
end
I have to store success info for each client in the loop so in case of error I know where to continue. The idea is to save client's ID in a table X when a message is successfully sent. If I did it with PHP i would control that manually (new db table for storing clients' ids).
How would you do that in Ruby on Rails? Do I really need a model for that?
You can still access the database directly in rails. Go ahead and make a migration and create your new table. There are many ways to run queries directly in rails. You can run them like this:
ActiveRecord::Base.connection.select_one('SELECT COUNT(*) FROM mytable')
or
ActiveRecord::Base.connection.execute('SELECT * FROM mytable')
Take a look at connection on ActiveRecord::Base for different ways to do it.
If you wanted something else, you could take a look at the Sequel gem.