Rails has_many through not returning records -
i trying setup , use first has_many through association in rails 4.2.
i have populated tables data , can see associated data across 3 tables, can't understand if issue model, associations, or table data.
the models are:
class user < activerecord::base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :user_lists has_many :lists, through: :user_lists accepts_nested_attributes_for :user_lists end class list < activerecord::base has_many :user_lists has_many :users, through: :user_lists end class userlist < activerecord::base belongs_to :user belongs_to :list validates_presence_of :user validates_presence_of :list end sample table data:
users (just showing id , email particular test user): id | email ----+------------------- 3 | antman@marvel.com user_lists: id | user_id | list_id ----+---------+--------- 13 | 1 | 1 14 | 2 | 2 15 | 2 | 3 16 | 3 | 4 17 | 3 | 5 18 | 3 | 6 lists: id | list_item_id | list_name | due_date ----+--------------+------------------------+---------- 7 | | 1: coles | 8 | | 2: monday way home | 9 | | 2: woolworths sandgate | 10 | | 3: iga way home | 11 | | 3: aldi way home | 12 | | 3: pool stuff | users controller:
def index @user = current_user end users view:
<table> <thead> <tr> <th>list</th> <th></th> <th></th> </tr> </thead> <tbody> <% @user.lists.each |list| %> <tr> <td><%= list.list_name %></td> <td><%= link_to 'show', user_list %></td> <td><%= link_to 'edit', edit_user_list_path(user_list) %></td> <td><%= link_to 'destroy', user_list, method: :delete, data: { `confirm: 'are sure?' } %></td> </tr> <% end %> </tbody> </table> i setup user_lists controller , view , viewed associated user_lists records (the join table) according logged in user.
when trying in rails console test "any" lists, , lists associated user (#3 in case) false , nothing:
irb(main):001:0> user=user.last user load (3.0ms) select "users".* "users" order "users"."id" desc limit 1 => #<user id: 3, first_name: nil, last_name: nil, username: nil, email: "antman@marvel.com",... irb(main):002:0> user.lists.any? list exists (0.8ms) select 1 one "lists" inner join "user_lists" on "lists"."id" = "user_lists"."list_id" "user_lists"."user_id" = $1 limit 1 [["user_id", 3]] => false irb(main):003:0> user.lists.all list load (0.6ms) select "lists".* "lists" inner join "user_lists" on "lists"."id" = "user_lists"."list_id" "user_lists"."user_id" = $1 [["user_id", 3]] => #<activerecord::associationrelation []> irb(main):004:0> my burning questions in hmt 101 are:
i think issue in underlying association or data - given rails console fails return records, case?
i think should using users controller/views display data - appropriate place, or should use controller/view?...also taking consideration thinking of implementing admin interface including master user list maintenance.
thankyou in advance! ll
in sample data provided, seems ids lists 4, 5, , 6 missing. if case, join table referring records no longer exist , not show in console.
for further help, try create new list, , give output of user.lists after saving record.
for example:
user = user.last list = user.lists.create!({ list_name: "4: test list" }) user.lists this allow see created list if associations working properly. if not, want review list see went wrong - whether it's join table record, or otherwise.
Comments
Post a Comment