0

I have the following relationship

Foo has_many Bar

Then i have the Foo model however the Bar is just a String which is an id. I could create a Bar model consisting only the String id, but i want to know if is possible and how to make Foo query all Bars and store it inside as a list in the Foo model.

If i have to use SQL it is fine.

Another alternative is to remove the Bar table and then put it as a field in the Foo table however this would require to serialize and would difficult and make SQL search inefficient guess.

The schema is the follwing

Foo has_many Foo_Bars and Bar has_many Bar_Foos

The reason for not been Foo has_and_belongs_to_many Bar is because the add information of a single Foo or a single Bar would be a never ending chain, think this as a bipartite graph such as Foo and Bar are a disjoint set.

The database is shared with another application i am using rails as a visual interface to manage the data.

My question is do i really need a Foo_Bars model ?

3
  • Will you ever have a random Bar that you will need to lookup which Foo it belongs to? Can a Bar belong to more than one Foo? If so, are you ever going to need to search for all of the Foo's having a specific Bar? Depending on what, exactly, Foo and Bar represent and their real-life relation, there are many ways this could go. Everything from serialized attributes in Foo to a has_and_belongs_to_many relationship with a join table. More info, please. Commented Apr 3, 2013 at 20:53
  • yes this was supose to be a has_and_belongs_to_many relationship but because in most scenarios the Foo entity does not need to lookup entire Bar information a the Bar.id is enough to set up the relationship. Also i forget to mention to get information of Bar is costly thats why i decided only the Bar.id is enough which cames with the Foo entity. There is a another table Bar which contains full information. Commented Apr 3, 2013 at 21:01
  • 1
    If you only want to pull the id, you can leave it as a 'Foo has_many Bars' relation and use 'foo.bars.all :select => :id'. That will create SQL equivalent to: 'select id from bars where bars.foo_id = #{foo.id}'. As long as the foreign key bars.foo_id is indexed, it should be pretty efficient. Commented Apr 3, 2013 at 21:40

2 Answers 2

2

Sounds like you'll have to create a bar model. Rails default primary id is an integer, not a string. You seem to want to list all Bars that 'belong_to' a Foo. So your migration will look like this:

rails g model bar foo_id:integer

Now inside /models/bar.rb:

class Bar < ActiveRecord::Base
  belongs_to :foo
end

Your models/foo.rb:

class Foo < ActiveRecord::Base
  has_many :bars
end

create some Bar records with foo foreign ids, and you can now query all the bars that belong to foo by this:

foo = Foo.find id
foo.bars

or from Bar:

Bar.where(:foo_id => id)

Read and practice this: http://guides.rubyonrails.org/active_record_querying.html

Sign up to request clarification or add additional context in comments.

Comments

1

You can have something like this method in Foo model

class Foo < ActiveRecord::Base
  serialize :barlist

  ...

  def store_barlist
    update_attributes(:barlist => self.bars.map {|bar| bar.string_id })
  end
end

To call this in controller, you'd have to do something like this.

def some_controller_action
  @foo = Foo.find(1)
  @foo.store_barlist
end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.