Hi I''m very new to Rails, and was wondering whether I''d have any issues with the following or if there are any better ways of doing it. I have a users table, which has a role field (char(1)). I also have a stores table, where each store belongs_to a user, but a user can only "has_many" stores if their role is set to ''p'' (for partner). Can I still put has_many :stores in my User model even though only 1 type of user can have them? Or is there a better/easier way of doing this. Thanks Elliot
Hi,
One way that comes to mind is to place the belongs_to and has_many
lines in your models, but also add a few methods to you user model
that will allow for the creation/deletion of the store, checking the
role when you do it.
Something like this:
class User < ActiveRecord::Base
has_many :stores
def new_store(store)
if self.role == ''p''
self.stores << store
return true
else
return false
end
end
end
That''s one way - there''s probably a better way...
--
Ben Myles
RailsAppHosting
www.railsapphosting.com
Got your free beta account yet?
On 8/1/05, Elliot Bowes <eblists-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi
>
> I''m very new to Rails, and was wondering whether I''d have
any issues
> with the following or if there are any better ways of doing it.
>
> I have a users table, which has a role field (char(1)).
> I also have a stores table, where each store belongs_to a user, but a
> user can only "has_many" stores if their role is set to
''p'' (for
> partner).
>
> Can I still put has_many :stores in my User model even though only 1
> type of user can have them? Or is there a better/easier way of doing
> this.
>
> Thanks
>
> Elliot
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
On Jul 31, 2005, at 3:21 PM, Elliot Bowes wrote:> Hi > > I''m very new to Rails, and was wondering whether I''d have any issues > with the following or if there are any better ways of doing it. > > I have a users table, which has a role field (char(1)). > I also have a stores table, where each store belongs_to a user, but a > user can only "has_many" stores if their role is set to ''p'' (for > partner).You may want to consider using single table inheritance, as well. It may or may not work for you--I obviously don''t know your environment and requirements--but consider: 1) Instead of a "role", a user has a "type" 2) The type field names the User subclass that represents the user (e.g. "Partner") 3) Define your User class. 4) Define your Partner class as a subclass of User, and put the "has_many :stores" specification in your Partner class. - Jamis
Elliot Bowes wrote:> Hi > > I''m very new to Rails, and was wondering whether I''d have any issues > with the following or if there are any better ways of doing it. > > I have a users table, which has a role field (char(1)). > I also have a stores table, where each store belongs_to a user, but a > user can only "has_many" stores if their role is set to ''p'' (for > partner). > > Can I still put has_many :stores in my User model even though only 1 > type of user can have them? Or is there a better/easier way of doing > this.One particularly nice way to do this is to use views. Create a view "partners" with a definition something like: select * from users where role = ''p'' Then create a model for partners which has a one-many relationship to stores. R. -- http://robinbowes.com If a man speaks in a forest, and his wife''s not there, is he still wrong?