I'm working on a Rails app that will serve as an authentication system for other Rails apps through Rails's ActiveResource functionality.
The authentication app has an ActiveRecord model called User. The client app has an ActiveResource model called User. I know that in the client app I can do stuff like user.save and it will perform a PUT operation using XML over HTTP.
But what if I want to put into my client app's User model has_many :contacts or something like that (contacts being an ActiveRecord model in the client app)? Then I'd want to do stuff like get all the Contacts that belong to some User.
Is that possible?
(I noticed that a similar question was asked, but there weren't many responses.)
Short answer: no. The classes involved in has_many, belongs_to, has_and_belongs_to_many live in ActiveRecord and build SQL queries to make the associations work.
That said, you can make it look like there is an association, you just have to write your own methods. Which was the up-voted answer on that question you linked to.
So, add a column to your Contact model that is user_id or whatever key you need to pass to your User.find ActiveResource model and you could part of the association contract like this:
class User < ActiveResource::Base
# boilerplate ActiveResource url stuff
def contacts
Contact.find(:all, :conditions => { :user_id => self.id })
end
end
class Contact < ActiveRecord::Model
def user
User.find_by_user_id(self.user_id)
end
end
There's a lot more you get from has_many for free, but that's the gist of it.
This question is ancient, but I ran across this recently:
http://yetimedia.tumblr.com/post/35233051627/activeresource-is-dead-long-live-activeresource
From the post:
Basic support for associations: has_many, has_one, belongs_to now can be used in ActiveResource models.
There are some other improvements noted in the posting that may warrant a second look into ActiveResource even though it has been removed from Rails 4.0.
Related
I need some advice on my user model. I came across the STI design and implemented it using devise. This is the basic setup:
class Message < ActiveRecord
has_one :sender
has_one :recipient
# Devise user
class User < ActiveRecord
class Sender < User
belongs_to :message
class Recipient < User
belongs_to :message
My conundrum is that the same user can be both sender and recipient in different scenarios. I originally set up this domain model such that a message record had a sender_id and recipient_id both of which were simply user_ids without any Railsy relationships defined or devise extras.
My previous solution seemed more flexible but the STI design seems more elegant and if possible I'd like to make it work. As I understand it the convention is that the type field discerns which user is returned. Is there a common solution for this using STI?
Broadly speaking, every time I've ended up implementing multiple types of user model, I've ended up regretting it. As you've already noticed, very quickly you end up with people who want to be both types of user, and suddenly you've got to manage different accounts, repeat logins, duplicate emails, etc.
I recommend instead setting up a single type of user that has_and_belongs_to_many roles. For simple cases, you can simply create your own role models and logic (which is what I've usually done), but it looks like this gem is pretty well supported as well: https://github.com/RolifyCommunity/rolify
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 users and contacts.
Users sign into their account and can create address books from which they add contacts to.
If they share something with a contact that contact needs to login to view it.
Users are unique in the db (stored in users table) and contacts are stored in the contacts table and are only unique per a given address book.
I'm currently using the Sorcery gem for users, which is working great. However, how can I extend this to support authentication for contact to login?
I've read a bit into doing this via STI or polymorphic setup, but unclear on what the general pattern is for something like this.
Can I simply have both models use Sorcery? Or is that an anti-pattern?
Thanks in advance!
Why are you using a separate model for contacts? Why not just set up a self-join like:
has_many :contact_entries, :class_name => "ContactEntry"
has_many :contacts, :through => :contact_entries
Your user table would look the same, but you would have a join model like:
class ContactEntry < ActiveRecord::Base
belongs_to :user
belongs_to :contact, :foreign_key => "contact_id", :class_name => "User"
end
Which would have a user_id and contact_id field.
Update
Okay, I see your issue now. I don't think this will be possible with sorcery, at least not without making substantial edits to sorcery itself. Sorcery defines a single authenticatable model in the file initializer.rb.
Authlogic, however, can be brought into any model via "acts_as_authentic", so it is a plausible solution to your needs. The drawback is that authlogic doesn't seem to be actively developed. It had a fair amount of activity 10 days ago, however, so it's definitely worth looking in to.
I am using the gem postmarkdown to create a blog in RoR. The Post model in the gem is not backed by a database (it uses ActiveModel). How would I go about relating a Comment model to the Post model for a blog that does not utilize a database for the blog posts?
For example, with a typical blog backed by an ActiveRecord database, I could set up the relations (such as)
class Post < ActiveRecord::Base
has_many :comments
However, in this case, I don't know the best way to create a comment model.
If Post is an activemodel, you can't setup relations using methods in activerecord. You can check out the README at github. It doesn't have that functionality.
One way you could to is simply define your own methods inside Post model.
class Post
def comments
Comment.where(:post_id => id)
end
end
class Comment < ActiveRecord::Base
def post
Post.find_by_id(post_id)
end
end
Edit:
Ah, I just find a similar question, Ruby on Rails 3 (3.1) ActiveModel Associations (tableless nested models). You can check out that as well.
I'm building a Rails 3 application that will have user-specific settings (looks, functionality, etc) and I was seeking some simple advice on whats the preferred way of actually implementing settings.
Do you prefer to have a dedicated model for this stuff? Are hashes acceptable to store in a database field? Do you prefer cookies or sessions over the database? Is an STI object best?
Maybe list some pros or cons to each different method if you can.
Thanks.
i've same situation like you, user specific setting. In my apps i prefer creating a model to store user's configuration i've User model and User_configuration model, where the relationship is one-to-one.
class User < ActiveRecord::Base
has_one :user_configuration
end
class UserConfiguration < ActiveRecord::Base
belongs_to :user, :dependent => :destroy
end
Or if you prefer using Hash and store it to database is possible to mark your field as serialize
class User < ActiveRecord::Base
serialize :preferences, Hash
end
you can see it at http://api.rubyonrails.org/classes/ActiveRecord/Base.html
pros:
- so far i've doesn't have any problem, it easy to maintenance
cons:
- request more table in database
May be it could help you thanks.
If you want some structured solution you can either have a look at:
Configurable Engine
or rails-settings