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.
Related
i'm starting to learn rails, and i'm creating a little website, but i don't know how can i get a list from database.
i have 3 tables:
user
pills
dependent
class User < ActiveRecord::Base
has_many:user_dependents
end
class Dependent < ActiveRecord::Base
has_many:pill_dependents
has_many:user_dependents
end
class PillDependent < ActiveRecord::Base
belongs_to:pill
belongs_to:dependent
end
How can i get the list of Pills, from all the dependents connected with the user?
If I'm understandig correctly, you want to define a transitions table connecting Users and Dependents. In rails you can map this relation automatically by using has_many :through. After you ran the migrations, you can access the Users from a Dependent by running for example:
dependent = Dependent.first
users = dependent.users
And the other way arround
user = Users.first
dependents = user.dependents
Rails does involve a lot of magic. It is necessary to learn about this magic by some tutorials. Otherwise there remain things you just won't get.
I'd recommend rails for zombies from codeschool. They're very good in explaining the magic.
Before I start, let me excuse myself for asking such a basic question, but I really didn't find any suitable information.
So, I have two ActiveRecord Models, Managers and Orders:
class Manager < ActiveRecord::Base
attr_accessible ...
has_many :orders
class Order < ActiveRecord::Base
belongs_to :manager
I have a backbone collection, which perfectly fetches managers. But what I don't get is how to get my manager's orders. Is there a solution for that or should I handle this manually?
If you want to fetch a list of managers, with each manager having a list of their orders in JSON format, I highly recommend the rabl gem. It makes it very easy to set this up and if needed customize what's included in the JSON, whether you're using Backbone, KnockoutJS or something else on the front end.
I have a container class I need to name, today it is called "EnrollmentApplication". It is a container of one type of application.
I've decided to push some of the business logic specific to different types of applications into a delegate associated with the application via a polymorphic relationship, to allow support for different types of applications. I'm struggling with a good name for the class and the relationship:
today:
EnrollmentApplication and applicationable
It's no longer just an application, but a collection of tasks and steps , kind of a workflow.
Portfolio and portfoliable....
ApplicationContainer and containable....
thanks
Joel
If the name of your class is called Enrollment then the following could work:
class Enrollment < ActiveRecord::Base
has_many :enrollment_applications
has_many :enrollments, :through => :enrollment_applications
end
Then you need your join table:
class EnrollmentApplication < Active Record::Base
belongs_to :enrollment
end
I really don't know if this will work because rails might complain about only having one id. I'm gonna try this when I get home to see if you can do this because I am actually setting up and app where user has many users. Anyway, keep us posted on your progress.
I was hoping I could get feedback on major changes to how a model works in an app that is in production already.
In my case I have a model Record, that has_many PhoneNumbers.
Currently it is a typical has_many belongs_to association with a record having many PhoneNumbers.
Of course, I now have a feature of adding temporary, user generated records and these records will have PhoneNumbers too.
I 'could' just add the user_record_id to the PhoneNumber model, but wouldn't it be better for this to be a polymorphic association?
And if so, if you change how a model associates, how in the heck would I update the production database without breaking everything? >.<
Anyway, just looking for best practices in a situation like this.
Thanks!
There's two approaches that might help you with this.
One is to introduce an intermediate model which handles collections of phone numbers. This way your Record and UserRecord can both belong_to this collection model and from there phone numbers and other contact information can be associated. You end up with a relationship that looks like this:
class Record < ActiveRecord::Base
belongs_to :address_book
delegate :phone_numbers, :to => :address_book
end
class UserRecord < ActiveRecord::Base
belongs_to :address_book
delegate :phone_numbers, :to => :address_book
end
class AddressBook < ActiveRecord::Base
has_many :phone_numbers
end
This kind of re-working can be done with a migration and a bit of SQL to populate the columns in the address_books table based on what is already present in records.
The alternative is to make UserRecord an STI derived type of Record so you don't need to deal with two different tables when defining the associations.
class Record < ActiveRecord::Base
has_many :phone_numbers
end
class UserRecord < Record
end
Normally all you need to do is introduce a 'type' string column into your schema and you can use STI. If UserRecord entries are supposed to expire after a certain time, it is easy to scope their removal using something like:
UserRecord.destroy_all([ 'created_at<=?', 7.days.ago ])
Using the STI approach you will have to be careful to scope your selects so that you are retrieving only permanent or temporary records depending on what you're intending to do. As UserRecord is derived from Record you will find they get loaded as well during default loads such as:
#records = Record.find(:all)
If this causes a problem, you can always use Record as an abstract base class and make a derived PermanentRecord class to fix this:
class PermanentRecord < Record
end
Update during your migration using something like:
add_column :records, :type, :string
execute "UPDATE records SET type='PermanentRecord'"
Then you can use PermanentRecord in place of Record for all your existing code and it should not retrieve UserRecord entries inadvertently.
Maintenance page is your answer.
Generate migration which updates table structure and updates existing data. If you're against data updates in migrations - use rake task.
Disable web access (create maintenance page)
Deploy new code
Run pending migrations
Update data
Enable web access (remove maintenance page).
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.