Hi everyone, I want to create a model that uses two tables for its data. Is that possible or just a bad idea? I don''t want to do the has_one because I want to avoid the extra dereferencing. For example Model A - attributes name in first table - attribute phone_num in second table Thanks, Frank -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frank Kim wrote:> Hi everyone, > > I want to create a model that uses two tables for its data. Is that > possible or just a bad idea? I don''t want to do the has_one because I > want to avoid the extra dereferencing.I usually avoid doing this, but... "I want to create a creature with two hands. Is that possible or a bad idea? I don''t want to link them to the same area of the brain because I want to avoid putting five fingers on each hand". I mean, maybe I''m completely off, but it sounds like you just want to create an extra table. What possible benefit could you derive from this, if the data won''t be separate? I guess, if both your tables have 200 fields, you would eventually derive an increase in read speeds if you don''t need all the data.. But I can''t think of another reason. You _can_ do it. Sometimes it''s just a really bad idea, but you can always do it. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Okay here''s why I want to do it. The first table contains just data. The second table will be a view whose data can change depending on other external factors. It could change daily. Yes this second table could be an association but I would have preferred it not to be. On Mon, Mar 15, 2010 at 10:18 AM, Aldric Giacomoni <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > I mean, maybe I''m completely off, but it sounds like you just want to > create an extra table. What possible benefit could you derive from this, > if the data won''t be separate? I guess, if both your tables have 200 > fields, you would eventually derive an increase in read speeds if you > don''t need all the data.. But I can''t think of another reason. > > You _can_ do it. Sometimes it''s just a really bad idea, but you can > always do it. > ---- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frank Kim wrote:> Okay here''s why I want to do it. > The first table contains just data. > The second table will be a view whose data can change depending on > other external factors. It could change daily. > Yes this second table could be an association but I would have > preferred it not to be.Well, then it really is two separate things, and it should be two separate things. You are right. Update the second table every so often with a scheduled task. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Mar 15, 2010 at 12:18 PM, Aldric Giacomoni <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> What possible benefit could you derive from thisA second table is required when you have constraints that you don''t want interfering with your ability to save data to the first table. has_one is very useful for this scenario. -- Greg Donald destiney.com | gregdonald.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 March 2010 18:14, Greg Donald <gdonald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Mar 15, 2010 at 12:18 PM, Aldric Giacomoni <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> What possible benefit could you derive from this > > A second table is required when you have constraints that you don''t > want interfering with your ability to save data to the first table. > has_one is very useful for this scenario.I can''t tell what your angle is from this post - are you disagreeing with Aldric, or the OP? A has_one is the association the OP said he didn''t want; and the idea of a second table with no association is what Aldric seems to be querying. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frank Kim wrote:> Okay here''s why I want to do it. > The first table contains just data. > The second table will be a view whose data can change depending on > other external factors. It could change daily. > Yes this second table could be an association but I would have > preferred it not to be.I don''t think ActiveRecord is going to be your friend here. AFAIK ActiveRecord expects a model to represent a single database table. For instance you can override the conventional mapping of a model to a table using; set_table_name "my_table_name" However, I know of no way to set multiple table names to one model. You can certainly use more than one model mapped to a single table (STI). But, not the other way around (again AFAIK). Beside this, much of the functionality of ActiveRecord would no longer work. How would it know which tables are represented by the model, and which attributes belongs to which table. It would not have the benefit of a join in order to determine what goes where. Just my 2 cents. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 March 2010 17:22, Frank Kim <railsonly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Okay here''s why I want to do it. > The first table contains just data. > The second table will be a view whose data can change depending on > other external factors. It could change daily. > Yes this second table could be an association but I would have > preferred it not to be.Why would you prefer it not to be? You are presumably going to have some sort of key linking the two tables otherwise how do you know which rows in the tables go together. In that case what overhead is there to using two models? Rails should be able to build essentially the same queries as you would do yourself. Colin> > On Mon, Mar 15, 2010 at 10:18 AM, Aldric Giacomoni <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> I mean, maybe I''m completely off, but it sounds like you just want to >> create an extra table. What possible benefit could you derive from this, >> if the data won''t be separate? I guess, if both your tables have 200 >> fields, you would eventually derive an increase in read speeds if you >> don''t need all the data.. But I can''t think of another reason. >> >> You _can_ do it. Sometimes it''s just a really bad idea, but you can >> always do it. >> -- > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Mar 15, 2010 at 10:07 AM, Frank Kim <railsonly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi everyone, > > I want to create a model that uses two tables for its data. Is that > possible or just a bad idea? I don''t want to do the has_one because I > want to avoid the extra dereferencing. > > For example > > Model A > - attributes name in first table > - attribute phone_num in second table > > Thanks, > Frank >Frank, the only reason that I can see having multiple tables would be in the following scenario: class User < AR has_many :phone_numbers end class PhoneNumber belongs_to :user end Now, both User and PhoneNumber would have there own individual database tables. Also, the phone_numbers table will require a foreign key (i.e. user_id). I would recommend reading the relevant sections on associations in "Agile Web Development with Rails 3rd" by Dave Thomas et al or consult the guides.rubyonrails.com. Good luck, -Conrad> > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mon, Mar 15, 2010 at 3:24 PM, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Mar 15, 2010 at 10:07 AM, Frank Kim <railsonly-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hi everyone, >> >> I want to create a model that uses two tables for its data. Is that >> possible or just a bad idea? I don''t want to do the has_one because I >> want to avoid the extra dereferencing. >> >> For example >> >> Model A >> - attributes name in first table >> - attribute phone_num in second table >> >> Thanks, >> Frank >> > > Frank, the only reason that I can see having multiple tables would be in > the following scenario: > > class User < AR > has_many :phone_numbers > end > > class PhoneNumber > belongs_to :user > end >The above should be class PhoneNumber < AR belongs_to :user end Note: AR is a shorthand for ActiveRecord::Base. -Conrad> Now, both User and PhoneNumber would have there own individual database > tables. Also, the phone_numbers table will require a foreign key (i.e. > user_id). I would recommend reading the relevant sections on associations > in "Agile Web Development with Rails 3rd" by Dave Thomas et al or consult > the guides.rubyonrails.com. > > Good luck, > > -Conrad > > >> >> -- >> >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks everyone for all your insight. It was very helpful. I was hoping to avoid dereferencing but I guess there''s no way around it. I will look into using has_one with a scheduled service for updating the secondary table. Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.