Has_many:through

Marnen Laibow-Koser wrote:

I think you understand the syntax. You just seem to be uncertain on
what methods are defined on the individual models and what’s defined on
the array as a whole.

That’s why I figured I would just see which methods were new since I
kept getting an undefined method error but as per my example above, the
array came up empty. So, according to that bit of code, my associations
are not giving me any new methods with which to work with. Again, maybe
I’m mistaken here.

What would the simplest check be to determine whether or not both models
are associated with one another?

Älphä Blüë wrote:
[…]

What would the simplest check be to determine whether or not both models
are associated with one another?

Use reflect_on_association (no, I won’t describe it here – read the
docs). That’s how I usually do it in my RSpec files

If you’re using Shoulda, I believe it has ready-made .should_belong_to
assertions.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

remove the .find(:all), you’ve already found @team so
@team.rushing_offenses is all you need.

-eric

On Jun 27, 8:24 am, “Älphä Blüë” [email protected]

On Jun 27, 3:13 pm, “Älphä Blüë” [email protected]
wrote:

@team = Team.find :all
@team.rushing_offenses

@team.rushing_offenses

NoMethodError: undefined method ‘rushing_offenses’ for
#Array:0x5ae469c

Here @team isn’t a team. It’s an array containing all of your teams so
you can’t call rushing_offenses (which is an instance method of Team)
on it - you’ve got to pull out one particular team and call it on
that.

Fred

Eric wrote:

remove the .find(:all), you’ve already found @team so
@team.rushing_offenses is all you need.

-eric

On Jun 27, 8:24�am, “�lph� Bl��” [email protected]

Thanks Eric, that’s what I had thought when I read everything Marnen
posted afterwards, which is why I tried to find out if maybe I was using
the wrong method.

But, for instance:

@team = Team.find :all
@team.rushing_offenses

@team.rushing_offenses
NoMethodError: undefined method ‘rushing_offenses’ for
#Array:0x5ae469c

2009/6/27 Älphä Blüë [email protected]:

posted afterwards, which is why I tried to find out if maybe I was using
the wrong method.

But, for instance:

@team = Team.find :all
@team.rushing_offenses

@team is (effectively) an array of Teams. It must be if you think
about it as you you have asked to find all the teams. You might be
better to call it @teams
Then you can do things like
@teams.each do |team|

some code using team.rushing_offenses

end

Again team.rushing_offenses is an array

Colin

Yay - I figured the paginate issue out on my own…

compiled_this_week.paginate :joins => :team, :conditions => [‘name like
?’, “%#{search}%”], :order => orderby + " #{orderall}", :per_page =>
numteams, :page => page

just had to add :joins

Very nifty now that I’m seeing how they tie in together. More
lightbulbs coming on…

Thanks guys,

So after a 36 hours the lightbulb finally clicked in. I think the
greatest issue I found throughout this particular issue wasn’t that I
didn’t hear what some of you were saying to me, it’s that I couldn’t see
what you were saying to me.

The end result code for finding everything from both associated tables
was:

@rushing_offenses.each do |r|
r.rank
r.team.name # this is the piece I did not understand how to code
r.games
r.etc…
r.etc…
end

I didn’t know how to tie in the two tables code wise when iterating
through each row. A lot of what I read concentrated more on talking
about how associations work but less on how to code a simple select all
rows statement. As I said beforehand, I work better when I see code. I
just happened to stumble upon this while doing my own tests… and then I
understood a bit more…

But, I still have an issue with my list method and using code between
two tables…

For instance,

I have a named_scope for the date between compiles:

named_scope :compiled_this_week, lambda { { :conditions => ['compiled_on

? and compiled_on < ?’, Time.now.beginning_of_week,
Time.now.end_of_week] } }

… and a list function to paginate my code:

def self.list(search, page, orderby, sortby, numteams)
orderby = “rank” if orderby == nil
orderall = (sortby != ‘asc’ && sortby != nil) ? “DESC” : “ASC”
compiled_this_week.paginate :conditions => [‘name like ?’,
“%#{search}%”], :order => orderby + " #{orderall}", :per_page =>
numteams, :page => page
end

In my index view I’m making a search call to :name,

    <% form_tag rushing_offenses_path, :method => 'get' do %>
      <p>
        <%= hinted_text_field_tag :search, params[:search], "Enter 

Team" %>
<%= submit_tag “Team Search”, :name => nil %>


<% end %>

But of course the :name is not going to work now. I still do not know
how to change the pagination to work between two tables.

If I find this answer, my brain will not explode but I’ll finally be
able to take a shower and my fiance won’t make me sleep on the couch
tonight… :slight_smile:

By the way, Marnen - loved your site and the fact that you are into
SCA…

Colin, Fred, and Eric - all four of you have helped me through a bunch
of things so far. I only hope that one day I’ll be knowledable enough
that I can start answering other peoples’ questions…